LoginSignup
12
15

More than 5 years have passed since last update.

PyEnv を使ったPythonのインストールしたメモ

Posted at

phpenv は仕事の都合上よく使うけど、PyEnvをそういや使ったこと無いので使って見るテスト。

おもっきり rbenv の Python 版として作られたコマンドなので、コマンドオプションなどは同じです。

結構前からあったみたいなので、以前ボクが書いた CentOS に Python2.7, Python3.3を入れたメモの代替として。

$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv

実行後、.zshenv もしくは .bash_profile に下記のようなのを書く。( .bashrc には書いてはいけない。無限ループに陥るっぽい Github issue #264。README.mdしか見ていないので追っかけていないけど。 )

if [ -e "$HOME/.pyenv" ]; then
  export PYENV_ROOT="$HOME/.pyenv"
  export PATH="$PYENV_ROOT/bin:$PATH"
  eval "$(pyenv init -)"
fi

記述後、設定ファイルを読み込む

$ source .bash_profile
$ source .zshenv

以上でPyEnv のインストールは終わり。

ちなみにMacOSX版は、HomeBrew で brew install pyenv です。

PyEnv を使った Python のインストール

pypy3-2.4.0 および 3.5.0 をインストールしてみる。

$ pyenv install 3.5.0
Downloading Python-3.5.0.tgz...
-> https://yyuu.github.io/pythons/584e3d5a02692ca52fce505e68ecd77248a6f2c99adf9db144a39087336b0fe0
Installing Python-3.5.0...
Installed Python-3.5.0 to /home/yasui/.pyenv/versions/3.5.0


$ pyenv install pypy3-2.4.0
Downloading pypy3-2.4-linux_x86_64-portable.tar.bz2...
-> https://bitbucket.org/squeaky/portable-pypy/downloads/pypy3-2.4-linux_x86_64-portable.tar.bz2
Installing pypy3-2.4-linux_x86_64-portable...
Installing pip from https://bootstrap.pypa.io/get-pip.py...
Installed pypy3-2.4-linux_x86_64-portable to /home/yasui/.pyenv/versions/pypy3-2.4.0

$ pyenv versions
* system (set by /home/yasui/.pyenv/version)
  3.5.0
  pypy3-2.4.0
$ python --version
Python 2.6.6
$ which python
~/.pyenv/shims/python

インストールはできたっぽいので、3.5.0 をメインに使ってみる。

$ pyenv global 3.5.0
~ $ python --version
Python 3.5.0
~ $ which python
~/.pyenv/shims/python

使い方みたいなの

特定のプロジェクトで、Pythonのバージョンに依存する時は pyenv local で作成される .python-version をGitリポジトリに入れておき、サーバ側でデプロイなどをするや他の開発者の方にインストールさせる時に、そのリポジトリで pyenv install を実行すれば、そのプロジェクトで使うバージョンのPythonが入ります。

リポジトリ側

~/test1 $ pyenv global
3.5.0
~/test1 $ ls                                                                                                                                                                                                                                                      git:master ?
./  ../  .git/
~/test1 $ pyenv local 2.7.10
~/test1 $ ls
./  ../  .git/  .python-version
$ git commit -a -m 'add .python-version'                                                                                                                                                                                                                   git:master
[master (root-commit) b40425f] add .python-version
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 .python-version

インストール側

~/test $ cat .python-version
2.7.10
~/test $ pyenv versions
pyenv: version `2.7.10' is not installed
  system
  3.5.0
  pypy3-2.4.0
~/test $ pyenv install
Downloading Python-2.7.10.tgz...
-> https://yyuu.github.io/pythons/eda8ce6eec03e74991abb5384170e7c65fcd7522e409b8e83d7e6372add0f12a
Installing Python-2.7.10...
patching file ./Lib/site.py
Installed Python-2.7.10 to /home/yasui/.pyenv/versions/2.7.10


~/test $ python --version
Python 2.7.10

後は、そのプロジェクトに移動するたびに pyenv のshell が自動的にいいバージョンにしてくれます。

~ $ python --version
Python 3.5.0
~ $ cd test
~/test $ python --version
Python 2.7.10
12
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
15