一:pyenv介绍

 
 
pyenv lets you easily switch between multiple versions of Python. It's simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well.
简单的说,pyenv 可以根据需求使用户在系统里安装和管理多个 Python 版本:
  1. 配置当前用户的python的版本
  2. 配置当前shell的python版本
  3. 配置某个项目(目录及子目录)的python版本
  4. 配置多个虚拟环境
 二:工作原理介绍

pyenv是利用系统环境变量PATH的优先级,劫持python的命令到pyenv上,根据用户所在的环境或目录,使用不同版本的python。
how it works:
At a high level, pyenv intercepts Python commands using shim executables injected into your PATH, determines which Python version has been specified by your application, and passes your commands along to the correct Python installation.
    对于系统环境变量 PATH ,里面包含了一串由冒号分隔的路径,例如 /usr/local/bin:/usr/bin:/bin。每当在系统中执行一个命令时,例如 python 或 pip,操作系统就会在 PATH 的所有路径中从左至右依次寻找对应的命令。因为是依次寻找,因此排在左边的路径具有更高的优先级。在PATH 最前面插入一个 $(pyenv root)/shims 目录,$(pyenv root)/shims目录里包含名称为python以及pip等可执行脚本文件;当用户执行python或pip命令时,根据查找优先级,系统会优先执行shims目录中的同名脚本。pyenv 正是通过这些脚本,来灵活地切换至我们所需的Python版本。
更详细的资料,可以查看pyenv的文档介绍或者源码实现。
三:pyenv的安装

系统环境:CentOS 6.10
1.安装依赖环境
yum -y installgcc zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel git
2.安装pyenv包
pyenv可以通过多种方式安装,可以参考项目在github上的Installtion。推荐采用The automatic installer的方式安装,可以一键安装pyenv的所有插件。
The automatic installer
Visit my other project: https://github.com/pyenv/pyenv-installer
一键安装:
分析一下上面的pyenv-installer脚本,可以发现在centos上,其实它做了以下事情:
git clone --depth 1"git://github.com/pyenv/pyenv.git"            "${HOME}/.pyenv"
git clone --depth 1"git://github.com/pyenv/pyenv-doctor.git"     "${HOME}/.pyenv/plugins/pyenv-doctor"
git clone --depth 1"git://github.com/pyenv/pyenv-installer.git"  "${HOME}/.pyenv/plugins/pyenv-installer"
git clone --depth 1"git://github.com/pyenv/pyenv-update.git"     "${HOME}/.pyenv/plugins/pyenv-update"
git clone --depth 1"git://github.com/pyenv/pyenv-virtualenv.git""${HOME}/.pyenv/plugins/pyenv-virtualenv"
git clone --depth 1"git://github.com/pyenv/pyenv-which-ext.git"  "${HOME}/.pyenv/plugins/pyenv-which-ext"
3.pyenv安装完成
安装完成后,还需要执行下面的命令,将pyenv安装到系统环境变量中。
echo 'export PATH="/home/python/.pyenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
source ~/.bash_profile
#查看pyenv版本
pyevn -v 
#更新pyenv
pyenv update
 
四:pyenv的使用

pyenv的命令,可以通过pyenv help查看,更详细的信息,可以查看文档:https://github.com/pyenv/pyenv/blob/master/COMMANDS.md
$ pyenv help
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/pyenv/pyenv#readme
 
1.使用pyen安装python
使用 pyenv install --list 查看可以安装的python版本,pyenv install  <version> 安装python,可以使用-v参数,查看安装过程。更多详细信息,使用 python install help命令了解 
$pyenv install --help
Usage: pyenv install [-f] [-kvp] <version>
pyenv install [-f] [-kvp] <definition-file>
pyenv install -l|--list
pyenv install --version
-l/--list List all available versions
-f/--force Install even if the version appears to be installed already
-s/--skip-existing Skip 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)
-p/--patch Apply a patch from stdin before building
-v/--verbose Verbose mode: print compilation status to stdout
--version Show version of python-build
-g/--debug Build a debug version
For detailed information on installing Python versions with
python-build, including a list of environment variables for adjusting
$ pyenv install --list
Available versions:
2.1.3
2.2.3
2.3.7
………………
$ pyenv install 3.6.4
Downloading Python-3.6.4.tar.xz...
Installing Python-3.6.4...
Installed Python-3.6.4 to /home/python/.pyenv/versions/3.6.4
$ pyenv install 3.5.4
Downloading Python-3.5.4.tar.xz...
Installing Python-3.5.4...
Installed Python-3.5.4 to /home/python/.pyenv/versions/3.5.4
 2.查看使用的python版本
参数 versions,可以查看pyenv已经安装及可管理python版本,前面带星号(*)表示当前使用的版本;也可以使用version查看当前使用的版本
#查看可使用的版本,前面带*表示当前使用的版本
pyenv versions
#查看当前使用的版本
pyenv version
 
 
$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.5.4
3.6.4
$ pyenv version
system (set by /home/python/.pyenv/version)
3.配置及管理python版本
pyenv管理python版本的三个基础命令
  • pyenv global <version>
  • pyenv shelll <version>
  • pyenv local <version>
1)使用pyenv global <version>配置当前用户的系统使用的python版本
$ pyenv versions
* system
  3.5.4
  3.6.4 (set by /home/python/.pyenv/version)
$ python -V
Python 2.6.6
$ pyenv global 3.6.4
$ pyenv versions
  system
  3.5.4
* 3.6.4 (set by /home/python/.pyenv/version)
$ python -V
Python 3.6.4
$ python -V
Python 3.6.4
$ exit
logout
#重新登录
$ python -V
Python 3.6.4
2)使用pyenv shelll <version>配置当前shell的python版本,退出shell则失效
查看当前的python版本及可用版本
$ python -V
Python 2.6.6
 
$ pyenv versions
* system (set by /home/python/.pyenv/version)
  3.5.4
  3.6.4
使用pyenv shell <version>命令配置当前shell的python版本
$ pyenv shell 3.5.4
查看配置以后的python版本
$ python -V
Python 3.5.4
$ pyenv versions
  system
* 3.5.4 (set by PYENV_VERSION environment variable)
  3.6.4
当前shell下,取消配置的使用python shell --unset;若退出此shell,配置也会失效。
$ pyenv shell --unset
3)使用pyenv local <version>配置所在项目(目录)的python版本
新建一个文件夹~/project,在此文件夹下使用python local <version>
$ mkdir project
$ cd project
$ pyenv local 3.6.4
在此文件夹下,查看版本
$ pyenv versions
  system
  3.5.4
* 3.6.4 (set by /home/python/project/.python-version)
 
$ python -V
Python 3.6.4
新建目录~/project/p_a,切换到~/project/p_a,并查看版本
$ mkdir p_a&& cd p_a
 
$ python -V
Python 3.6.4
$ pyenv versions
  system
  3.5.4
* 3.6.4 (set by /home/python/project/.python-version)
新建目录~/project/p_b,切换到~/project/p_b,使用pyenv local system,并查看版本
$ mkdir ../p_b&& cd ../p_b
 
$ pyenv local system
$ python -V
Python 2.6.6
$ pyenv versions
* system (set by /home/python/project/p_b/.python-version)
  3.5.4
  3.6.4
 
4.稍微深入一下,pyenv是如何进行python版本管理的
使用which命令,可以看到,python命令已经不是本来的python命令,而是shims中的脚本文件
$ which python
~/.pyenv/shims/python
$ which python3
~/.pyenv/shims/python3
$ which pip3
~/.pyenv/shims/pip3
 
 
查看~/.pyenv/shims/python,可以看到python的命令最终被~/.pyenv/libexec/pyenv接管运行,感兴趣的同学可以接着看~/.pyenv/libexec/pyenv代码的实现,这里就不从代码上分析了。
#!/usr/bin/env bash
set -e
[ -n "$PYENV_DEBUG" ] && set -x
 
program="${0##*/}"
if [[ "$program" = "python"* ]]; then
  for arg; do
    case "$arg" in
    -c* | -- ) break ;;
    */* )
      if [ -f "$arg" ]; then
        export PYENV_FILE_ARG="$arg"
        break
      fi
      ;;
    esac
  done
fi
 
export PYENV_ROOT="/home/python/.pyenv"
exec "/home/python/.pyenv/libexec/pyenv" exec "$program" "$@"
 
pyenv的文档,对此是如此介绍的:
pyenv works by inserting a directory of shims at the front of your PATH:
 
$(pyenv root)/shims:/usr/local/bin:/usr/bin:/bin
Through a process called rehashing, pyenv maintains shims in that directory to match every Python command across every installed version of Python—python, pip, and so on.
 
Shims are lightweight executables that simply pass your command along to pyenv. So with pyenv installed, when you run, say, pip, your operating system will do the following:
 
Search your PATH for an executable file named pip
Find the pyenv shim named pip at the beginning of your PATH
Run the shim named pip, which in turn passes the command along to pyenv
 
Choosing the Python Version
 
When you execute a shim, pyenv determines which Python version to use by reading it from the following sources, in this order:
 
The PYENV_VERSION environment variable (if specified). You can use the pyenv shell command to set this environment variable in your current shell session.
 
The application-specific .python-version file in the current directory (if present). You can modify the current directory's .python-version file with the pyenv local command.
 
The first .python-version file found (if any) by searching each parent directory, until reaching the root of your filesystem.
 
The global $(pyenv root)/version file. You can modify this file using the pyenv global command. If the global version file is not present, pyenv assumes you want to use the "system" Python. (In other words, whatever version would run if pyenv weren't in your PATH.)
 
NOTE: You can activate multiple versions at the same time, including multiple versions of Python2 or Python3 simultaneously. This allows for parallel usage of Python2 and Python3, and is required with tools like tox. For example, to set your path to first use your system Python and Python3 (set to 2.7.9 and 3.4.2 in this example), but also have Python 3.3.6, 3.2, and 2.5 available on your PATH, one would first pyenv install the missing versions, then set pyenv global system 3.3.6 3.2 2.5. At this point, one should be able to find the full executable path to each of these using pyenv which, e.g. pyenv which python2.5 (should display $(pyenv root)/versions/2.5/bin/python2.5), or pyenv which python3.4 (should display path to system Python3). You can also specify multiple versions in a .python-version file, separated by newlines or any whitespace.
 大概的意思是说,当使用的python命令被pyenv接管以后,到底使用哪个python版本,是由下面这些信息依次决定的:
  • 如果PYENV_VERSION这个变量否存在,则使用这个变量里的版本;这个变量是由pyenv shell <version>配置的;
  • 按照往父目录查找的顺序查找直到根目录,第一个被查找到的.python-version文件作为版本文件,其指定的版本作为使用的python版本;这个文件使用pyenv local <version>配置
  • $(pyenv root)/version 这个文件若存在,则使用这个文件里制定的版本作为python版本;若不存在,则使用系统的版本;这个文件使用pyenv global <version>配置
  • 如果以上变量或文件都没有找到,就按照系统默认制定的python版本了。
另外,用户还可以在一个环境下同时配置多个版本的python;具体的解释和用法,可以参考文档。
回头再看一下上面pyenv versions命令,输出的结果中会有一个set by的提示,也向用户展示了,pyenv是基于什么指定的python版本。
 
五:python的虚拟环境

 
为了对不同的项目进行隔离,使每个项目使用独立的解释器及依赖,需要配置python虚拟环境。本机已经安装了pyenv-virtualenv
使用pyenv install安装的python版本,比如3.6.4;解释器安装的路径为~/.pyenv/versions/3.6.4/;插件的安装的路径为~/.pyenv/versions/3.6.4/lib/python3.6/site-packages;
使用pyenv-virtualenv创建python虚拟环境,实质上是在~/.pyenv/versions/3.6.4/下创建一个文件夹evns,存放该虚拟环境python的解释器;并且在~/.pyenv/下创建一个软连接,该虚拟环境可以通过pyenv进行管理;
$ pyenv virtualenv 3.6.4 my_3.6.4
Requirement already satisfied: setuptools in /home/python/.pyenv/versions/3.6.4/envs/my_3.6.4/lib/python3.6/site-packages
Requirement already satisfied: pip in /home/python/.pyenv/versions/3.6.4/envs/my_3.6.4/lib/python3.6/site-packages
 
$ll ~/.pyenv/versions/
total 8
drwxr-xr-x 6 python python 4096 Jul 20 00:59 3.5.4
drwxr-xr-x 7 python python 4096 Jul 21 01:03 3.6.4
lrwxrwxrwx 1 python python   48 Jul 21 01:03 my_3.6.4 -> /home/python/.pyenv/versions/3.6.4/envs/my_3.6.4
 
 
查看python虚拟环境
$ pyenv virtualenvs
  3.6.4/envs/my_3.6.4 (created from /home/python/.pyenv/versions/3.6.4)
  my_3.6.4 (created from /home/python/.pyenv/versions/3.6.4)
 
 
切换到python虚拟环境
$ pyenv shell my_3.6.4
(my_3.6.4) [python@localhost 3.6.4]$
(my_3.6.4) [python@localhost 3.6.4]$ pyenv versions
  system
  3.5.4
  3.6.4
  3.6.4/envs/my_3.6.4
* my_3.6.4 (set by PYENV_VERSION environment variable)
 
 
 
 
 
 
 
05-17 01:31