Mac OSX python多版本管理工具:pyenv 和 virtualenv搭建

    • Installation pyenv
      • 方法1使用Mac OSX的Homebrew安装
      • 方法2通过github工程安装
    • Installation pyenv-virtualenv
    • Using pyenv-virtualenv
    • 特定python版本的某个环境下pip安装包
    • Install issues
      • ERROR pyenv install -v 351 failed to download Python-351targz
      • pip install error
    • References

pyenv 是轻量的Python版本管理器,帮助你在一台机子上建立多个版本的python环境,并提供方便的切换方法。

pyenv-virtualenv 是 pyenv的扩展工具(类Unix系统上),可以搭建虚拟且独立的python环境,可以使每个项目环境与其他项目独立开来,保持环境的干净,解决包冲突问题。

更多pyenv和virtualenv的工作原理,请查看github工程说明。(引用1和2)

本文主要讲解Mac OSX上安装pyenv和virtualenv的过程,并记录安装中碰到的问题和相应的解决方法。

环境:OSX10.11 + shell: zsh + iTerm2

Installation pyenv

方法1:使用Mac OSX的Homebrew安装

$ brew update
$ brew install pyenv

方法2:通过github工程安装

  1. 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

  2. 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
  • 切换python版本
    pyenv global 3.5.1pyenv global system

每次安装完新版本python,或新的虚拟环境,或新的包,可能需要执行命令 pyenv rehash 才能生效

Installation pyenv-virtualenv

同安装pyenv- ,有两种方法安装virtualenv,这里只讲解github方法安装

  1. Check out pyenv-virtualenv into plugin directory
    $ git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv

  2. (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

Using pyenv-virtualenv

pyenv virtualenv my-virtualenv-3.5.1
pyenv activate <name>
pyenv deactivate
pyenv uninstall my-virtual-env-3.5.1

特定python版本的某个环境下pip安装包

先切换到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)]

测试成功.

Install issues

ERROR: pyenv install -v 3.5.1 failed to download Python-3.5.1.tar.gz

Error:

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

  1. 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
    
  2. 手动下载Python-3.5.1.tar.gz复制到~/.pyenv/versions下
  3. 最后执行命令:pyenv install -v 3.5.1

pip install error

```
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

References

  1. https://github.com/yyuu/pyenv
  2. https://github.com/yyuu/pyenv-virtualenv
  3. http://my.oschina.net/davehe/blog/625896
  4. pip install Error: http://blog.csdn.net/lijiang1991/article/details/51775896

你可能感兴趣的:(Python)