pyenv 是轻量的Python版本管理器,帮助你在一台机子上建立多个版本的python环境,并提供方便的切换方法。
pyenv-virtualenv 是 pyenv的扩展工具(类Unix系统上),可以搭建虚拟且独立的python环境,可以使每个项目环境与其他项目独立开来,保持环境的干净,解决包冲突问题。
更多pyenv和virtualenv的工作原理,请查看github工程说明。(引用1和2)
本文主要讲解Mac OSX上安装pyenv和virtualenv的过程,并记录安装中碰到的问题和相应的解决方法。
环境:OSX10.11 + shell: zsh + iTerm2
$ brew update
$ brew install pyenv
Check out pyenv where you want it installed. A good place to choose is $HOME/.pyenv (but you can install it somewhere else).
$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv
Define environment variable PYENV_ROOT to point to the path where pyenv repo is cloned and add $PYENV_ROOT/bin
to your $PATH
for access to the pyenv command-line utility.
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
Zsh note: Modify your ~/.zshrc file instead of ~/.bash_profile. Ubuntu note: Modify your ~/.bashrc file instead of ~/.bash_profile.
3. Add pyenv init to your shell to enable shims and autocompletion. Please make sure eval “$(pyenv init -)” is placed toward the end of the shell configuration file since it manipulates PATH during the initialization.
4. Restart your shell so the path changes take effect. You can now begin using pyenv.
$ $SHELL -l
或 $ exec $SHELL
5. Install Python versions into $PYENV_ROOT/versions
. For example, to download and install Python 3.5.1, run:
查看有哪些版本可供安装: $ pyenv install --list
Available versions:
2.6.9
2.7-dev
2.7
…….
3.5.0
3.5-dev
3.5.1
3.5.2
……
安装
$ pyenv install 3.5.1
卸载
pyenv uninstall 3.5.1
查看所有版本( * 表示当前使用的版本)
☁ ~ pyenv versions
system
* 3.5.1 (set by /Users/jerry/.pyenv/version)
3.5.1/envs/my-virtualenv-3.5.1
my-virtualenv-3.5.1
pyenv global 3.5.1
或 pyenv global system
每次安装完新版本python,或新的虚拟环境,或新的包,可能需要执行命令 pyenv rehash
才能生效
同安装pyenv- ,有两种方法安装virtualenv,这里只讲解github方法安装
Check out pyenv-virtualenv into plugin directory
$ git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
(OPTIONAL) Add pyenv virtualenv-init to your shell to enable auto-activation of virtualenvs. This is entirely optional but pretty useful. See “Activate virtualenv” below.
$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.zshrc
Zsh note: Modify your ~/.zshenv file instead of ~/.bash_profile. Pyenv note: You may also need to add eval “$(pyenv init -)” to your profile if you haven’t done so already.
3. Restart your shell to enable pyenv-virtualenv
$ $SHELL -l
或 $ exec $SHELL
pyenv virtualenv my-virtualenv-3.5.1
pyenv activate <name>
pyenv deactivate
pyenv uninstall my-virtual-env-3.5.1
先切换到xx python版本的 xx virtualenv下
如:
☁ ~ pyenv global 3.5.1
☁ ~ pyenv version
3.5.1 (set by /Users/jerry/.pyenv/version)
☁ ~ pyenv activate my-virtualenv-3.5.1
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(my-virtualenv-3.5.1) ☁ ~ pip install numpy
新建文件hello.py
import sys
import numpy
print(sys.version)
执行如下命令:
(my-virtualenv-3.5.1) ☁ TestPy3 python hello.py
3.5.1 (default, Jul 5 2016, 01:08:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)]
测试成功.
pyenv install -v 3.5.1
failed to download Python-3.5.1.tar.gzError:
Downloading Python-3.5.1.tgz...
-> https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz
error: failed to download Python-3.5.1.tar.gz
BUILD FAILED (OS X 10.11 using python-build 20160509)
Resolve:
https://github.com/yyuu/pyenv/issues/303
http://stackoverflow.com/questions/37227854/pyenv-build-failed-ubuntu-15-04-using-python-build-20160509
In the centos 5.10,we should set the Setting http://yyuu.github.io/pythons as PYTHON_BUILD_MIRROR_URL in the .bashrc. then it runs ok. thank you very much
~/.zshrc
export PYTHON_BUILD_MIRROR_URL=”http://yyuu.github.io/pythons”
#jerry start pyenv
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
export PYTHON_BUILD_MIRROR_URL="http://yyuu.github.io/pythons"
eval "$(pyenv init -)"
#pyenv end
pyenv install -v 3.5.1
```
pip._vendor.requests.packages.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed out.
You are using pip version 7.1.2, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
```
Resolve:
http://blog.csdn.net/lijiang1991/article/details/51775896