1 pyenv说明
pyenv 可以根据需求使用户在系统里安装和管理多个 Python 版本,pyenv项目地址:https://github.com/pyenv/pyenv。
pyenv 可以实现如下功能:
1)配置当前用户的python的版本
2)配置当前shell的python版本
3)配置某个项目(目录及子目录)的python版本
4)配置多个虚拟环境
pyenv是利用系统环境变量PATH的优先级,劫持python的命令到pyenv上,根据用户所在的环境或目录,使用不同版本的python。
对于系统环境变量 PATH ,里面包含了一串由冒号分隔的路径,例如 /usr/local/bin:/usr/bin:/bin。每当在系统中执行一个命令时,例如 python 或 pip,操作系统就会在 PATH 的所有路径中从左至右依次寻找对应的命令。因为是依次寻找,因此排在左边的路径具有更高的优先级。在PATH 最前面插入一个 $(pyenv root)/shims 目录,$(pyenv root)/shims目录里包含名称为python以及pip等可执行脚本文件;当用户执行python或pip命令时,根据查找优先级,系统会优先执行shims目录中的同名脚本。pyenv 正是通过这些脚本,来灵活地切换至我们所需的Python版本。
2 pyenv 安装
我这里的Linux 版本是Redhat 6.10。
安装依赖环境
yum -y install gcc zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel git
安装pyenv包,具体安装可以参考github,这里采用automatic installer:
[root@www.cndba.cn ~]# curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
102 2454 102 2454 0 0 90 0 0:00:27 0:00:26 0:00:01 991
Initialized empty Git repository in /root/.pyenv/.git/
remote: Enumerating objects: 5106, done.
remote: Counting objects: 100% (5106/5106), done.
remote: Compressing objects: 100% (2081/2081), done.
remote: Total 5106 (delta 3685), reused 3913 (delta 2718), pack-reused 0
Receiving objects: 100% (5106/5106), 1.22 MiB | 11 KiB/s, done.
Resolving deltas: 100% (3685/3685), done.
Initialized empty Git repository in /root/.pyenv/plugins/pyenv-doctor/.git/
remote: Enumerating objects: 19, done.
remote: Counting objects: 100% (19/19), done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 19 (delta 4), reused 11 (delta 3), pack-reused 0
Unpacking objects: 100% (19/19), done.
Initialized empty Git repository in /root/.pyenv/plugins/pyenv-installer/.git/
remote: Enumerating objects: 26, done.
remote: Counting objects: 100% (26/26), done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 26 (delta 4), reused 14 (delta 0), pack-reused 0
Unpacking objects: 100% (26/26), done.
Initialized empty Git repository in /root/.pyenv/plugins/pyenv-update/.git/
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 10 (delta 1), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (10/10), done.
Initialized empty Git repository in /root/.pyenv/plugins/pyenv-virtualenv/.git/
remote: Enumerating objects: 517, done.
remote: Counting objects: 100% (517/517), done.
remote: Compressing objects: 100% (331/331), done.
remote: Total 517 (delta 342), reused 298 (delta 160), pack-reused 0
Receiving objects: 100% (517/517), 267.02 KiB | 49 KiB/s, done.
Resolving deltas: 100% (342/342), done.
Initialized empty Git repository in /root/.pyenv/plugins/pyenv-which-ext/.git/
remote: Enumerating objects: 20, done.
remote: Counting objects: 100% (20/20), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 20 (delta 3), reused 16 (delta 2), pack-reused 0
Unpacking objects: 100% (20/20), done.
WARNING: seems you still have not added 'pyenv' to the load path.
# Load pyenv automatically by adding
# the following to ~/.bashrc:
export PATH="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
[root@www.cndba.cn ~]#
这里最后提示我们配置环境变量,在 ~/.bashrc 添加上面的内容。
[root@www.cndba.cn ~]# cat ~/.bashrc
……
export PATH="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
[root@www.cndba.cn ~]#
[root@www.cndba.cn ~]# source ~/.bashrc
查看pyenv版本
[root@www.cndba.cn ~]# pyenv -v
pyenv 1.2.16
更新pyenv
[root@www.cndba.cn ~]# pyenv update
3 pyenv 工具使用说明
pyenv 工具支持一下命令:
[root@www.cndba.cn ~]# pyenv -h
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
commands List all available pyenv commands
activate Activate virtual environment
commands List all available pyenv commands
deactivate Deactivate virtual environment
doctor Verify pyenv installation and development tools to build pythons.
exec Run an executable with the selected Python version
global Set or show the global Python version
help Display help for a command
hooks List hook scripts for a given pyenv command
init Configure the shell environment for pyenv
install Install a Python version using python-build
local Set or show the local application-specific Python version
prefix Display prefix for a Python version
rehash Rehash pyenv shims (run this after installing executables)
root Display the root directory where versions and shims are kept
shell Set or show the shell-specific Python version
shims List existing pyenv shims
uninstall Uninstall a specific Python version
version Show the current Python version and its origin
--version Display the version of pyenv
version-file Detect the file that sets the current pyenv version
version-name Show the current Python version
version-origin Explain how the current Python version is set
versions List all Python versions available to pyenv
virtualenv Create a Python virtualenv using the pyenv-virtualenv plugin
virtualenv-delete Uninstall a specific Python virtualenv
virtualenv-init Configure the shell environment for pyenv-virtualenv
virtualenv-prefix Display real_prefix for a Python virtualenv version
virtualenvs List all Python virtualenvs found in `$PYENV_ROOT/versions/*'.
whence List all Python versions that contain the given executable
which Display the full path to an executable
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
这里演示一下常见操作。
3.1 安装Python
查看命令帮助:
[root@www.cndba.cn ~]# pyenv install -h
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
查看当前可以安装版本:
[root@www.cndba.cn ~]# pyenv install --list
Available versions:
……
2.6.6
2.6.7
2.6.8
2.6.9
2.7.0
2.7-dev
2.7.1
2.7.2
2.7.3
……
2.7.17
3.0.1
3.1.0
3.1.1
……
3.8.0
3.8-dev
3.8.1
3.9-dev
安装python 2.6.6, 这个版本也是linux 6.x 中的默认版本:
[root@www.cndba.cn ~]# pyenv install 2.6.6
Downloading Python-2.6.6.tgz...
-> https://www.python.org/ftp/python/2.6.6/Python-2.6.6.tgz
Installing Python-2.6.6...
……
Installing pip from https://bootstrap.pypa.io/2.6/get-pip.py...
Installed Python-2.6.6 to /root/.pyenv/versions/2.6.6
安装python 2.7.6, 这个版本也是linux 7.x 中的默认版本:
[root@www.cndba.cn ~]# pyenv install 2.7.6
Downloading Python-2.7.6.tgz...
-> https://www.python.org/ftp/python/2.7.6/Python-2.7.6.tgz
……
Installing pip from https://bootstrap.pypa.io/get-pip.py...
Installed Python-2.7.6 to /root/.pyenv/versions/2.7.6
3.2 查看版本
查看当前使用的版本
[root@www.cndba.cn ~]# pyenv version
system (set by /root/.pyenv/version)
查看可使用的版本,前面带*表示当前使用的版本
[root@www.cndba.cn ~]# pyenv versions
* system (set by /root/.pyenv/version)
2.6.6
2.7.6
[root@www.cndba.cn ~]#
3.3 配置及管理python版本
3.3.1 pyenv global
配置当前用户的系统使用的python版本。 这个全局配置影响较大,一般都是配置local 项目级别。
[root@www.cndba.cn ~]# pyenv versions
* system (set by /root/.pyenv/version)
2.6.6
2.7.6
[root@www.cndba.cn ~]# python -V
Python 2.6.6
[root@www.cndba.cn ~]# pyenv global 2.7.6
[root@www.cndba.cn ~]# pyenv versions
system
2.6.6
* 2.7.6 (set by /root/.pyenv/version)
[root@www.cndba.cn ~]# python -V
Python 2.7.6
[root@www.cndba.cn ~]#
通过设置,可以发现系统的python 版本从2.6.6 升级到2.7.6了。
3.3.2 pyenv shelll
配置当前shell的python版本,退出shell则失效。
[root@www.cndba.cn ~]# python -V
Python 2.7.6
[root@www.cndba.cn ~]# pyenv versions
system
2.6.6
* 2.7.6 (set by /root/.pyenv/version)
[root@www.cndba.cn ~]# pyenv shell 2.6.6
[root@www.cndba.cn ~]# python -V
Python 2.6.6
[root@www.cndba.cn ~]# pyenv versions
system
* 2.6.6 (set by PYENV_VERSION environment variable)
2.7.6
当前shell下,取消配置的使用python shell —unset;若退出此shell,配置也会失效。
[root@www.cndba.cn ~]# pyenv shell --unset
[root@www.cndba.cn ~]# pyenv versions
system
2.6.6
* 2.7.6 (set by /root/.pyenv/version)
[root@www.cndba.cn ~]#
注意这里unset 之后返回的还是global 设置的python 版本。
3.3.3pyenv local
该命令用来配置所在项目(目录)的python版本,一般使用该方法进行配置。
新建目录并修改python 版本:
[root@www.cndba.cn ~]# mkdir orz
[root@www.cndba.cn ~]# cd orz/
[root@www.cndba.cn orz]# pyenv local 2.6.6
[root@www.cndba.cn orz]# pyenv versions
system
* 2.6.6 (set by /root/orz/.python-version)
2.7.6
[root@www.cndba.cn orz]#
退出该目录,就是默认的python 版本了:
[root@www.cndba.cn orz]# cd ..
[root@www.cndba.cn ~]# python -V
Python 2.7.6
[root@www.cndba.cn ~]#
[root@www.cndba.cn ~]# pyenv versions
system
2.6.6
* 2.7.6 (set by /root/.pyenv/version)
[root@www.cndba.cn ~]#
4 python的虚拟环境
pyenv整合virtualenv 后可以实现真正意义上的环境隔离,每个项目都相当于使用一个单独的解释器。 我们在之前自动安装pyenv 时已经安装过了virtualenv的插件。 所以直接创建虚拟环境即可。
[root@www.cndba.cn ~]# cd ~/.pyenv/plugins/
[root@www.cndba.cn plugins]# ll
总用量 24
drwxr-xr-x. 4 root root 4096 1月 13 03:43 pyenv-doctor
drwxr-xr-x. 6 root root 4096 1月 13 03:43 pyenv-installer
drwxr-xr-x. 5 root root 4096 1月 13 03:44 pyenv-update
drwxr-xr-x. 8 root root 4096 1月 13 03:44 pyenv-virtualenv
drwxr-xr-x. 4 root root 4096 1月 13 03:44 pyenv-which-ext
drwxr-xr-x. 5 root root 4096 1月 13 03:42 python-build
[root@www.cndba.cn plugins]#
使用pyenv-virtualenv创建python虚拟环境,实质上是在~/.pyenv/versions/2.7.6/下创建一个文件夹evns,存放该虚拟环境python的解释器;并且在~/.pyenv/下创建一个软连接,该虚拟环境可以通过pyenv进行管理。
[root@www.cndba.cn plugins]# pyenv virtualenv 2.7.6 orz2.7.6
New python executable in /root/.pyenv/versions/2.7.6/envs/orz2.7.6/bin/python2.7
Also creating executable in /root/.pyenv/versions/2.7.6/envs/orz2.7.6/bin/python
Installing setuptools, pip, wheel...
done.
Installing pip from https://bootstrap.pypa.io/get-pip.py...
……
Successfully installed pip-19.3.1
[root@www.cndba.cn plugins]#
[root@www.cndba.cn plugins]# ll ~/.pyenv/versions/
总用量 8
drwxr-xr-x. 6 root root 4096 1月 13 04:09 2.6.6
drwxr-xr-x. 7 root root 4096 1月 13 07:08 2.7.6
lrwxrwxrwx. 1 root root 41 1月 13 07:11 orz2.7.6 -> /root/.pyenv/versions/2.7.6/envs/orz2.7.6
[root@www.cndba.cn plugins]#
查看python虚拟环境
[root@www.cndba.cn plugins]# pyenv virtualenvs
2.7.6/envs/orz2.7.6 (created from /root/.pyenv/versions/2.7.6)
orz2.7.6 (created from /root/.pyenv/versions/2.7.6)
[root@www.cndba.cn plugins]#
切换到python虚拟环境:
[root@www.cndba.cn plugins]# pyenv shell orz2.7.6
(orz2.7.6) [root@www.cndba.cn plugins]#
(orz2.7.6) [root@www.cndba.cn plugins]# pwd
/root/.pyenv/plugins
(orz2.7.6) [root@www.cndba.cn plugins]# cd /
(orz2.7.6) [root@www.cndba.cn /]#
返回到系统环境:
(orz2.7.6) [root@www.cndba.cn ~]# pyenv shell system
[root@www.cndba.cn ~]#
也可以使用如下命令切换到新的虚拟环境:
[root@www.cndba.cn ~]# pyenv activate orz2.7.6
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(orz2.7.6) [root@www.cndba.cn ~]#
(orz2.7.6) [root@www.cndba.cn ~]#
(orz2.7.6) [root@www.cndba.cn ~]# python -V
Python 2.7.6
(orz2.7.6) [root@www.cndba.cn ~]#
切换到虚拟环境之后,命令行都会有虚拟环境的前缀。 在虚拟环境里折腾不会影响到真实的环境。
切换回系统环境:
(orz2.7.6) [root@www.cndba.cn ~]# pyenv deactivate
[root@www.cndba.cn ~]#
删除虚拟环境:
[root@www.cndba.cn ~]# pyenv virtualenv-delete orz2.7.6
pyenv-virtualenv: remove /root/.pyenv/versions/2.7.6/envs/orz2.7.6? yes
[root@www.cndba.cn ~]#
版权声明:本文为博主原创文章,未经博主允许不得转载。