pyenv的作用

pyenv lets you easily switch between multiple versions of Python.

pyenv的安装步骤

在这里,我们使用Homebrew来安装pyenv。

  • 执行brew install pyenv命令

    Alexs-Mac:~ alex$ brew install pyenv
    ==> Installing dependencies for pyenv: autoconf, pkg-config
    ==> Installing pyenv dependency: autoconf
    ==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/autoconf-2.
    ######################################################################## 100.0%
    ==> Pouring autoconf-2.69.mavericks.bottle.1.tar.gz
    🍺  /usr/local/Cellar/autoconf/2.69: 69 files, 2.0M
    ==> Installing pyenv dependency: pkg-config
    ==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/pkg-config-
    ######################################################################## 100.0%
    ==> Pouring pkg-config-0.28.mavericks.bottle.2.tar.gz
    🍺  /usr/local/Cellar/pkg-config/0.28: 10 files, 604K
    ==> Installing pyenv
    ==> Downloading https://github.com/yyuu/pyenv/archive/v0.4.0-20140404.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/20140404: 254 files, 2.4M, built in 23 seconds
    

    一定要注意上面输出中的告警提示

    ==> 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
    
  • 在~目录下使用touch建立.bash_profile文件

    touch .bash_profile
    
  • 使用MacVim编辑.bash_profile文件

    mvim .bash_profile
    

    内容如下:

    export PYENV_ROOT=/usr/local/opt/pyenv
    if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
    

    上面的内容将改变Pyenv root path并确保pyenv被初始化。

    注意:.bash_profile文件中,上面两个语句的顺序一定不能颠倒:
    (如果颠倒的话,pyenv global中的设置将会不起作用)

  • 使对.bash_profile的改变立即生效:

    $SHELL -l
    

    注意:一定要进行此步操作,否则在进行pyenv install 2.7.6时,
    Python会被安装到/Users/alex/.pyenv/versions/2.7.6下,
    而不是Homebrew的相关目录下:/usr/local/opt/pyenv/versions/2.7.6

使用pyenv安装Python 2.7.6

注意:pyenv命令中支持Tab补全。

  • 列出服务器上可被下载安装的所有的Python版本和各类发布

    pyenv install --list
    
  • 使用pyenv安装Python 2.7.6(OS X Mavericks中自带的Python版本是2.7.5)

    pyenv install 2.7.6
    

    过程如下:

    Alexs-Mac:~ alex$ pyenv install 2.7.6
    Downloading readline-6.2.tar.gz...
    -> http://yyuu.github.io/pythons/67948acb2ca081f23359d0256e9a271c
    Installing readline-6.2...
    Installed readline-6.2 to /usr/local/opt/pyenv/versions/2.7.6
    
    Downloading Python-2.7.6.tgz...
    -> http://yyuu.github.io/pythons/1d8728eb0dfcac72a0fd99c17ec7f386
    Installing Python-2.7.6...
    patching file ./Modules/readline.c
    Installed Python-2.7.6 to /usr/local/opt/pyenv/versions/2.7.6
    
    Downloading setuptools-3.4.1.tar.gz...
    -> https://pypi.python.org/packages/source/s/setuptools/setuptools-3.4.1.tar.gz
    Installing setuptools-3.4.1...
    Installed setuptools-3.4.1 to /usr/local/opt/pyenv/versions/2.7.6
    
    Downloading pip-1.5.4.tar.gz...
    -> https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz
    Installing pip-1.5.4...
    Installed pip-1.5.4 to /usr/local/opt/pyenv/versions/2.7.6
    
  • 执行pyenv rehash

    Run this command after you install a new version of Python,
    or install a package that provides binaries.

使用pyenv设置和切换Python版本

  • 查看当前可用的Python版本

    pyenv versions
    
  • 查看当前使用的Python版本

    pyenv version
    
  • 查看当前的pyenv设置情况

    pyenv global

    Sets the global version of Python to be used in all shells
    by writing the version name to the ~/.pyenv/version file.
    This version can be overridden by an application-specific .python-version
    file, or by setting the PYENV_VERSION environment variable.
    

    pyenv local

    Sets a local application-specific Python version
    by writing the version name to a .python-version file
    in the current directory.
    This version overrides the global version,
    and can be overridden itself by setting the PYENV_VERSION
    environment variable or with the pyenv shell command.
    

    pyenv shell

    Sets a shell-specific Python version
    by setting the PYENV_VERSION environment variable in your shell.
    This version overrides application-specific versions and the global version.
    

    所以说,一般情况下,使用pyenv global设置要使用的Python版本就足够了。

    使用pyenv刚安装完Python 2.7.6后的各项设置所使用Python版本的情况:

    Alexs-Mac:~ alex$ pyenv version
    system (set by /usr/local/opt/pyenv/version)
    
    Alexs-Mac:~ alex$ pyenv versions
    * system (set by /usr/local/opt/pyenv/version)
      2.7.6
    
    Alexs-Mac:~ alex$ pyenv global
    system
    
    Alexs-Mac:~ alex$ pyenv local
    pyenv: no local version configured for this directory
    
    Alexs-Mac:~ alex$ pyenv shell
    pyenv: no shell-specific version configured
    
  • pyenv shell中的Python版本设置只在当前的Shell会话中有效
    通过设置Shell的PYENV_VERSION环境变量

    pyenv shell中设置好Python版本后,
    退出Terminal后,会自动还原为未设置的状态

  • pyenv local中设置的Python版本只对相应的当前目录有效
    在目录中写入.python-version文件

  • pyenv global中设置的Python版本对所有的Shell全局有效,且退出后不会消失
    是通过设置/usr/local/opt/pyenv/version这个文件来实现的

    Alexs-Mac:~ alex$ pyenv global 2.7.6
    Alexs-Mac:~ alex$ pyenv versions
      system
    * 2.7.6 (set by /usr/local/opt/pyenv/version)
    
    Alexs-Mac:~ alex$ pyenv global system
    Alexs-Mac:~ alex$ pyenv versions
    * system (set by /usr/local/opt/pyenv/version)
      2.7.6
    
  • 一般情况下,只使用pyenv global和pyenv local就足够了

    Alexs-Mac:~ alex$ python
    Python 2.7.5 (default, Aug 25 2013, 00:04:04)
    [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> exit()
    
    Alexs-Mac:~ alex$ pyenv global 2.7.6
    Alexs-Mac:~ alex$ python
    Python 2.7.6 (default, Apr 17 2014, 13:46:37)
    [GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    
  • 其他一些不常使用的功能

    • 设置Shell中使用通过pyenv新安装的Python 2.7.6

      pyenv shell 2.7.6
      
    • 设置Shell中重新使用系统中Python版本(2.7.5)

      pyenv shell system
      
    • 取消Shell中的Python版本设置(恢复为默认状态)

      Alexs-Mac:~ alex$ pyenv shell --unset
      Alexs-Mac:~ alex$ pyenv shell
      pyenv: no shell-specific version configured
      

      注意:
      pyenv local和pyenv shell命令都有–unset参数,
      而pyenv global命令没有–unset参数

      Alexs-Mac:~ alex$ pyenv local --unset
      Alexs-Mac:~ alex$ pyenv local
      pyenv: no local version configured for this directory
      
      Alexs-Mac:~ alex$ pyenv shell --unset
      Alexs-Mac:~ alex$ pyenv shell
      pyenv: no shell-specific version configured
      
      Alexs-Mac:~ alex$ pyenv global --unset
      pyenv: version `--unset' not installed
      

参考