pyenv
Ruby使ってる人であれば「rbenvのPython版」で通じるかと。
つまり、Pythonの複数バージョンを切り替えれる環境をつくるものです。
今回はpyenvでPythonバージョンを変更したときに実行しないといけない「pyenv rehash」も自動で行ってくれる「pyenv-pip-rehash」も併せてインストールします。
インストール
毎度お馴染みhomebrewでインストールします。
$ brew install pyenv ==> Downloading https://github.com/yyuu/pyenv/archive/v0.4.0-20140211.tar.gz ######################################################################## 100.0% ==> Caveats To enable shims and autocompletion add to your profile: if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi To use Homebrew's directories rather than ~/.pyenv add to your profile: export PYENV_ROOT=/usr/local/opt/pyenv ==> Summary 🍺 /usr/local/Cellar/pyenv/20140211: 226 files, 2.3M, built in 5 seconds
次に.bashrcに
eval "$(pyenv init -)"
を追加します。( https://github.com/yyuu/pyenv#homebrew-on-mac-os-x 参照)
もしくはインストールメッセージにあったようにprofileファイル(私の場合はbashなので.bash_profileファイル)に
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
を追加します。
.bashrcの修正を適用させるために
$ . .bashrc
します。.bash_profileを書き替えた場合には
$ . .bash_profile
を。
pyenvインストール直後の環境確認
「pyenv versions」で現在インストールされているPythonを確認。
$ pyenv versions * system (set by /Users/riocampos/.pyenv/version)
当然ながらプリインストールされているPythonしか居ません。
pyenv-pip-rehashインストール
Never run pyenv rehash again. This pyenv plugin automatically runs pyenv rehash every time you install or uninstall packages with using pip.
GitHub - pyenv/pyenv-pip-rehash: Never run `pyenv rehash` again
ここに書いてあるようにgit cloneします。
homebrew でインストールできます。
$ brew install pyenv-pip-rehash ==> Downloading https://github.com/yyuu/pyenv-pip-rehash/archive/v0.0.4.tar.gz ######################################################################## 100.0% ==> ./install.sh 🍺 /usr/local/Cellar/pyenv-pip-rehash/0.0.4: 6 files, 24K, built in 4 seconds
インストール後は念のためターミナルを再起動させておきます。
ヘルプ
$ pyenv --help pyenv 0.4.0-20140211 Usage: pyenv <command> [<args>] Some useful pyenv commands are: commands List all available pyenv commands local Set or show the local application-specific Python version global Set or show the global Python version shell Set or show the shell-specific Python version install Install a Python version using python-build uninstall Uninstall a specific Python version rehash Rehash pyenv shims (run this after installing executables) version Show the current Python version and its origin versions List all Python versions available to pyenv which Display the full path to an executable whence List all Python versions that contain the given executable See `pyenv help <command>' for information on a specific command. For full documentation, see: https://github.com/yyuu/pyenv#readme
$ pyenv install --help Usage: pyenv install [-f] [-kvp] <version> pyenv install [-f] [-kvp] <definition-file> pyenv install -l|--list -l/--list List all available versions -f/--force Install even if the version appears to be installed already python-build options: -k/--keep Keep source tree in $PYENV_BUILD_ROOT after installation (defaults to $PYENV_ROOT/sources) -v/--verbose Verbose mode: print compilation status to stdout -p/--patch Apply a patch from stdin before building -g/--debug Build a debug version For detailed information on installing Python versions with python-build, including a list of environment variables for adjusting compilation, see: https://github.com/yyuu/pyenv#readme
基本的にrbenvと同じですね。
インストールできるバージョンの確認
$ pyenv install --list
Available versions:
2.4
2.4.1
2.4.2
2.4.3
2.4.4
2.4.5
2.4.6
2.5
2.5.1
2.5.2
2.5.3
2.5.4
2.5.5
2.5.6
2.6-dev
2.6.6
2.6.7
2.6.8
2.6.9
2.7
2.7-dev
2.7.1
2.7.2
2.7.3
2.7.4
2.7.5
2.7.6
3.0.1
3.1-dev
3.1.3
3.1.4
3.1.5
3.2
3.2-dev
3.2.1
3.2.2
3.2.3
3.2.4
3.2.5
3.3-dev
3.3.0
3.3.1
3.3.2
3.3.3
3.3.4
3.4-dev
3.4.0a2
3.4.0a3
3.4.0a4
3.4.0b1
3.4.0b2
3.4.0b3
anaconda-1.4.0
anaconda-1.5.0
anaconda-1.5.1
anaconda-1.6.0
anaconda-1.6.1
anaconda-1.7.0
anaconda-1.8.0
jython-2.5-dev
jython-2.5.0
jython-2.5.1
jython-2.5.2
jython-2.5.3
jython-2.5.4-rc1
jython-2.7-beta1
jython-dev
miniconda-2.2.2
miniconda-3.0.0
miniconda3-2.2.2
miniconda3-3.0.0
pypy-1.5
pypy-1.5-src
pypy-1.6
pypy-1.7
pypy-1.7-dev
pypy-1.8
pypy-1.8-dev
pypy-1.9
pypy-1.9-dev
pypy-2.0
pypy-2.0-dev
pypy-2.0-src
pypy-2.0.1
pypy-2.0.1-src
pypy-2.0.2
pypy-2.0.2-src
pypy-2.1
pypy-2.1-src
pypy-2.2
pypy-2.2-src
pypy-2.2.1
pypy-2.2.1-src
pypy-dev
pypy3-2.1-beta1
pypy3-2.1-beta1-src
pypy3-dev
stackless-2.7-dev
stackless-2.7.2
stackless-3.2-dev
stackless-3.2.2
stackless-3.3-dev
stackless-dev
Pythonインストール
Pythonをほとんど知らない私でも、バージョン2とバージョン3とで互換性が無いとは知っています。のでそれぞれの最新版を入れておきます。
2.7.6をインストール
$ pyenv install 2.7.6 Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Downloading Python-2.7.6.tgz... -> http://yyuu.github.io/pythons/1d8728eb0dfcac72a0fd99c17ec7f386 Installing Python-2.7.6... Installed Python-2.7.6 to /Users/riocampos/.pyenv/versions/2.7.6 Downloading setuptools-2.2.tar.gz... -> https://pypi.python.org/packages/source/s/setuptools/setuptools-2.2.tar.gz Installing setuptools-2.2... Installed setuptools-2.2 to /Users/riocampos/.pyenv/versions/2.7.6 Downloading pip-1.5.2.tar.gz... -> https://pypi.python.org/packages/source/p/pip/pip-1.5.2.tar.gz Installing pip-1.5.2... Installed pip-1.5.2 to /Users/riocampos/.pyenv/versions/2.7.6
3.3.4をインストール
$ pyenv install 3.3.4 Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Downloading Python-3.3.4.tgz... -> http://yyuu.github.io/pythons/9f7df0dde690132c63b1dd2b640ed3a6 Installing Python-3.3.4... Installed Python-3.3.4 to /Users/riocampos/.pyenv/versions/3.3.4 Downloading setuptools-2.2.tar.gz... -> https://pypi.python.org/packages/source/s/setuptools/setuptools-2.2.tar.gz Installing setuptools-2.2... Installed setuptools-2.2 to /Users/riocampos/.pyenv/versions/3.3.4 Downloading pip-1.5.2.tar.gz... -> https://pypi.python.org/packages/source/p/pip/pip-1.5.2.tar.gz Installing pip-1.5.2... Installed pip-1.5.2 to /Users/riocampos/.pyenv/versions/3.3.4
この直後の「pyenv versions」を確認。
$ pyenv versions * system (set by /Users/riocampos/.pyenv/version) 2.7.6 3.3.4
バージョン切り替え
rbenvとは異なる点として、pyenvでは複数のバージョンを指定可能です(ただ、おそらくは2と3とそれぞれから1つずつでしょうけど)。
You can specify multiple versions as global Python at once.
GitHub - pyenv/pyenv: Simple Python version management
$ pyenv global 3.3.4 2.7.6
できました。
確認しましょう。
$ pyenv versions system * 2.7.6 (set by /Users/riocampos/.pyenv/version) * 3.3.4 (set by /Users/riocampos/.pyenv/version)
$ python --version Python 3.3.4 $ python2.7 --version Python 2.7.6 $ python3.3 --version Python 3.3.4
完了
今後はpipもユーザ権限で実行可能になりますのでsudo不要になります。
参考サイト
補足:brew doctorでWarningが出る件について
pythonコマンドがバージョン3のpython3にリンクしてるよ、というWarning
Warning: python is symlinked to python3
This will confuse build scripts and in general lead to subtle breakage.
これは例えばバージョン2を優先するような設定をしてやれば問題ないです。
$ pyenv global 2.7.6 3.3.4
「"config" scripts exist outside your system or Homebrew directories.」
こんなメッセージです。
Warning: "config" scripts exist outside your system or Homebrew directories. `./configure` scripts often look for *-config scripts to determine if software packages are installed, and what additional flags to use when compiling and linking. Having additional scripts in your path can confuse software installed via Homebrew if the config script overrides a system or Homebrew provided script of the same name. We found the following "config" scripts: /Users/riocampos/.pyenv/shims/python-config /Users/riocampos/.pyenv/shims/python2-config /Users/riocampos/.pyenv/shims/python2.7-config /Users/riocampos/.pyenv/shims/python3-config /Users/riocampos/.pyenv/shims/python3.3-config /Users/riocampos/.pyenv/shims/python3.3m-config
pyenvを入れただけでは出ないエラーなのですが、CPython*1をpyenv installするとこのエラーが必ず出ます。
pyenv作者曰く「気にすんな」。上手く回避する方法が見つかっていないようです。
This can't be avoidable because pyenv manges shims for these *-config executables if there is CPython installed. I'd like to add some workarounds for this issue if available, but haven't found the way to deal with this yet.
pyenv not playing nice with brew "config". [$5] · Issue #106 · pyenv/pyenv · GitHub
You don't have to modify rc file. Just setting the version as system (e.g. pyenv shell system) before installing something with brew can force brew to use system python. Warnings from brew doctor can be safely ignorable in this case.
pyenv not playing nice with brew "config". [$5] · Issue #106 · pyenv/pyenv · GitHub
エラーが出ないようになってほしいですね。