pyenv virtualenv anacondaでpython2を指定する

pyenv virtualenvコマンドを使ってpython2のconda環境を構築しようとするとこれが意外に難しい。というのもdefaultでPython3がインストールされてしまうからだ。

スポンサーリンク

pyenv virtualenvでPython2のconda環境構築

以下のようにしてconda環境を構築しようとすると、

!pyenv virtualenv miniconda3-4.3.30 py27
Solving environment: done

## Package Plan ##

  environment location: /root/.pyenv/versions/miniconda3-4.3.30/envs/py27

  added / updated specs: 
    - python


The following NEW packages will be INSTALLED:

    ca-certificates: 2018.03.07-0           
    certifi:         2018.4.16-py36_0       
    libedit:         3.1.20170329-h6b74fdf_2
    libffi:          3.2.1-hd88cf55_4       
    libgcc-ng:       7.2.0-hdf63c60_3       
    libstdcxx-ng:    7.2.0-hdf63c60_3       
    ncurses:         6.1-hf484d3e_0         
    openssl:         1.0.2o-h20670df_0      
    pip:             10.0.1-py36_0          
    python:          3.6.5-hc3d631a_2       
    readline:        7.0-ha6073c6_4         
    setuptools:      39.2.0-py36_0          
    sqlite:          3.23.1-he433501_0      
    tk:              8.6.7-hc745277_3       
    wheel:           0.31.1-py36_0          
    xz:              5.2.4-h14c3975_4       
    zlib:            1.2.11-ha838bed_2      

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use:
# > source activate py27
#
# To deactivate an active environment, use:
# > source deactivate
#

Requirement already satisfied: setuptools in /root/.pyenv/versions/miniconda3-4.3.30/envs/py27/lib/python3.6/site-packages
Requirement already satisfied: pip in /root/.pyenv/versions/miniconda3-4.3.30/envs/py27/lib/python3.6/site-packages

python3.6.5が意に反してインストールされてしまう。miniconda2やminicondaを使っても同じような結果になる。minicondaやminiconda2はデフォがpython2なのにだ。

Python versionを指定してやればいい

単純にpython versionをpython2に指定してやることでこの問題は解消される。

!pyenv uninstall -f py27
!pyenv versions
  system
  3.6.5
  3.6.5/envs/py365
  caffe2
  miniconda3-4.3.30
  miniconda3-4.3.30/envs/caffe2
* py365 (set by PYENV_VERSION environment variable)
!pyenv virtualenv miniconda3-4.3.30 py27 --python=2.7
Solving environment: done

## Package Plan ##

  environment location: /root/.pyenv/versions/miniconda3-4.3.30/envs/py27

  added / updated specs: 
    - python=2.7


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    certifi-2018.4.16          |           py27_0         142 KB
    setuptools-39.2.0          |           py27_0         583 KB
    wheel-0.31.1               |           py27_0          62 KB
    python-2.7.15              |       h1571d57_0        12.1 MB
    pip-10.0.1                 |           py27_0         1.7 MB
    ------------------------------------------------------------
                                           Total:        14.6 MB

The following NEW packages will be INSTALLED:

    ca-certificates: 2018.03.07-0           
    certifi:         2018.4.16-py27_0       
    libedit:         3.1.20170329-h6b74fdf_2
    libffi:          3.2.1-hd88cf55_4       
    libgcc-ng:       7.2.0-hdf63c60_3       
    libstdcxx-ng:    7.2.0-hdf63c60_3       
    ncurses:         6.1-hf484d3e_0         
    openssl:         1.0.2o-h20670df_0      
    pip:             10.0.1-py27_0          
    python:          2.7.15-h1571d57_0      
    readline:        7.0-ha6073c6_4         
    setuptools:      39.2.0-py27_0          
    sqlite:          3.23.1-he433501_0      
    tk:              8.6.7-hc745277_3       
    wheel:           0.31.1-py27_0          
    zlib:            1.2.11-ha838bed_2      


Downloading and Extracting Packages
certifi-2018.4.16    |  142 KB | ####################################### | 100% 
setuptools-39.2.0    |  583 KB | ####################################### | 100% 
wheel-0.31.1         |   62 KB | ####################################### | 100% 
python-2.7.15        | 12.1 MB | ####################################### | 100% 
pip-10.0.1           |  1.7 MB | ####################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use:
# > source activate py27
#
# To deactivate an active environment, use:
# > source deactivate
#

Requirement already satisfied: setuptools in /root/.pyenv/versions/miniconda3-4.3.30/envs/py27/lib/python2.7/site-packages
Requirement already satisfied: pip in /root/.pyenv/versions/miniconda3-4.3.30/envs/py27/lib/python2.7/site-packages
!pyenv versions
  system
  3.6.5
  3.6.5/envs/py365
  caffe2
  miniconda3-4.3.30
  miniconda3-4.3.30/envs/caffe2
  miniconda3-4.3.30/envs/py27
  py27
* py365 (set by PYENV_VERSION environment variable)

非常に簡単な事なのに何故かマニュアルには載っていないのが残念だ。あまりにも当たり前過ぎて載せる必要もないのかもしれないが、意外とはまり所でもある。

参考サイトhttps://github.com/