pyenv是一个Python版本管理工具,可以管理多个版本的Python环境,
pyenv
可以用来管理和切换不同的python
版本,相比用virtualenv和anacondo来创建虚拟python环境,pyenv
的使用更简易轻便。
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
环境配置及初始化
# 在~/.bash_profile中添加下面
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
# 或通过echo命令追加到~/.bash_profile
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
生效配置
通常使用source命令重新载入配置文件
$ source ~/.bash_profile
验证pyenv是否正确安装
$ pyenv --help
使用pyenv
或pyenv --help
获取pyenv的帮助信息
pyenv [-h | --help]
$ pyenv
pyenv 2.3.6-7-g19359de7
...
$ pyenv --help
Usage: pyenv <command> [<args>]
...
通过pyenv install --list
查看pyenv当前支持哪些Python版本
$ pyenv install --list
通过pyenv install -v
命令来安装不同的Python版本
pyenv install -v
$ pyenv install -v 3.11.0 # Python 3.11 (stable)
$ pyenv install -v 3.6.15 # Python 3.6 (EOL)
...
Installed Python-3.6.15 to /home/zhuoyi/.pyenv/versions/3.6.15
$ pyenv install -v 2.7.18 # Python 2.7 (EOL)
...
Installed Python-2.7.18 to /home/zhuoyi/.pyenv/versions/2.7.18
使用pyenv versions
命令来查看当前系统中包含的Python版本
$ pyenv versions
* system (set by /home/zhuoyi/.pyenv/version)
2.7.18
3.6.15
说明:其中system
表示系统自身的Python, 其前面*
号表示当前正在使用此版本
通过pyenv global
命令来选择不同的Python版本
pyenv global
$ pyenv global 2.7.18
$ pyenv versions
system
* 2.7.18 (set by /home/zhuoyi/.pyenv/version)
3.6.15
$ python
Python 2.7.18 (default, Nov 12 2022, 10:15:22)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
$ pip --version
pip 19.2.3 from /home/zhuoyi/.pyenv/versions/2.7.18/lib/python2.7/site-packages/pip (python 2.7)
注意:切换Python版本以后,相应的pip也会更着切换;
$ pyenv global 3.6.15
$ pyenv versions
system
2.7.18
* 3.6.15 (set by /home/zhuoyi/.pyenv/version)
$ python
Python 3.6.15 (default, Nov 12 2022, 10:22:31)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
$ pip --version
pip 18.1 from /home/zhuoyi/.pyenv/versions/3.6.15/lib/python3.6/site-packages/pip (python 3.6)
通过pyenv uninstall
命令来删除指定的Python版本
pyenv uninstall
$ pyenv install -v 3.10.8
$ pyenv uninstall 3.10.8
$ pyenv --help
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
commands List all available pyenv commands
exec Run an executable with the selected Python version
global Set or show the global Python version(s)
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
latest Print the latest installed or known version with the given prefix
local Set or show the local application-specific Python version(s)
prefix Display prefixes for Python versions
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 Python versions
version Show the current Python version(s) 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
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
命令说明
commands -------------列出所有可用的pyenv命令
duplicate -------------创建一个重复的python环境
local --------------设置或显示特定于本地应用程序的Python版本
global --------------设置或显示全局Python版本
shell --------------设置或显示特定于shell的Python版本
install --------------Python构建安装Python版本
uninstall -------------卸载特定的Python版本
update -------------更新缓存的版本数据库
rehash -------------重新安装pyenv垫片(安装可执行文件后运行此操作)
vname -------------显示当前的Python版本
version -------------显示当前Python版本及其来源
version-name ----------------显示当前的Python版本
versions -----------------列出pyenv可用的所有Python版本
exec -----------------通过首先准备路径来运行可执行文件,以便选定的Python
which -------------- 显示可执行文件的完整路径
whence ---------------------列出包含给定可执行文件的所有Python版本
常用命令
pyenv versions:列出当前系统中所有安装的python。
pyenv version:显示出当前使用的python。
pyenv global <python_version>:设置使用哪一个python。
pyenv install <python_version>:安装特定版本的python。
pyenv uninstall <python_version>:移除特定版本的python。
pyenv install -l:查看可安装的python。
pyenv-virtualenv是virtualenv项目用在pyenv中一个插件,用以隔离不同项目的工作环境,
$ git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
环境配置及初始化
# 基于pyenv的配置
$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
生效配置文件
$ source ~/.bash_profile
验证pyenv-virtualenv是否正确安装
$ pyenv help virtualenv
为同一个Python解释器,创建多个不同的工作环境
pyenv virtualenv
$ pyenv virtualenv 2.7.18 first_project
$ pyenv virtualenv 2.7.18 second_project
使用pyenv virtualenvs
命令查看工作环境
$ pyenv virtualenvs
2.7.18/envs/first_project (created from /home/zhuoyi/.pyenv/versitongns/2.7.18)
2.7.18/envs/second_project (created from /home/zhuoyi/.pyenv/versions/2.7.18)
first_project (created from /home/zhuoyi/.pyenv/versions/2.7.18)
second_project (created from /home/zhuoyi/.pyenv/versions/2.7.18)
通过 activate进入和deactivate退出 工作环境
pyenv activate
$ pyenv activate first_project
pyenv-virtualenv: prompt changing will be removed from future release.
configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(first_project) zhuoyi@ubuntu:~$ pip install flask==1.0
(first_project) zhuoyi@ubuntu:~$ pyenv deactivate
zhuoyi@ubuntu:~$
pyenv deactivate
$ pyenv activate second_project
pyenv-virtualenv: prompt changing will be removed from future release.
configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(second_project) zhuoyi@ubuntu:~$ pip install flask==1.1
(second_project) zhuoyi@ubuntu:~$ pyenv deactivate
zhuoyi@ubuntu:~$
$ pyenv --help
... # 省略了原pyenv的
activate Activate virtual environment
deactivate Deactivate virtual environment
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/*'.
$ pyenv help virtualenv
Usage: pyenv virtualenv [-f|--force] [VIRTUALENV_OPTIONS] [version] <virtualenv-name>
pyenv virtualenv --version
pyenv virtualenv --help
-f/--force Install even if the version appears to be installed already. Skip
prompting for confirmation
Notable VIRTUALENV_OPTIONS passed to venv-creating executable, if applicable:
-u/--upgrade Imply --force