Ubuntu下用pyenv安装并管理Python多版本

pyenv使您可以轻松地在多个版本的Python之间切换,该项目是从rbenv和 ruby-build派生的,并针对Python进行了修改。

安装pyenv

1.安装pyenv

git clone https://github.com/pyenv/pyenv.git ~/.pyenv

2.定义环境变量PYENV_ROOT以指向上一步克隆pyenv repo的路径,并将其添加$PYENV_ROOT/bin到您的目录中$PATH以访问pyenv命令行实用程序。

For Ubuntu Desktop:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc

3.添加pyenv init到您的shell以启用shims和自动完成功能。请确保eval "$(pyenv init -)"将其放置在外壳程序配置文件的末尾,因为它会PATH在初始化期间进行操作。

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bashrc

4.重新启动shell程序,以便路径更改生效。 您现在可以开始使用pyenv。

exec "$SHELL"

5.安装Python构建依赖

Ubuntu/Debian/Mint:

sudo apt-get update; sudo apt-get install --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

6.将Python版本安装到中$(pyenv root)/versions 例如,要下载并安装Python 3.8.4,请运行:

pyenv install 3.8.4

升级pyenv

升级到最新的开发版(可能不稳定),使用git pull:

cd $(pyenv root)
git pull

要升级到特定版本的pyenv,请查看相应的标签:

cd $(pyenv root)
git fetch
git tag
git checkout v0.1.0

卸载pyenv

1.要禁用 pyenv管理您的Python版本,只需pyenv init从Shell启动配置中删除该行即可 。这将从PATH中删除pyenv shims目录,并且像pythonpyenv之前一样,将来的调用 将执行系统Python版本。

pyenv 仍然可以在命令行上访问,但是您的Python应用程序不会受到版本切换的影响。

2.要完全卸载 pyenv,请执行步骤(1),然后删除其根目录。这将删除$(pyenv root)/versions/目录下安装的所有Python版本

rm -rf $(pyenv root)

相关命令

#安装Python3.8.4
pyenv install 3.8.4
#卸载Python3.8.4
pyenv uninstall 3.8.4
#查看已经安装的python版本(列出所有的版本)
pyenv versions 
#查看当前使用的python版本
pyenv version
#显示 local/global Python版本,local/global二选一
pyenv local/global 
#设置local/global Python版本,如将Python设置为3.8.4,local/global二选一
pyenv local/global 3.8.4

可通过在终端中输入pyenv查看该命令的使用帮助

ubuntu:~/environment $ pyenv -h
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   --version   Display the version of pyenv
   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
   local       Set or show the local application-specific Python version(s)
   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(s) and its origin
   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

原创文章,作者:huoxiaoqiang,如若转载,请注明出处:https://www.huoxiaoqiang.com/experience/jingyan/1318.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注