Jean's Blog

一个专注软件测试开发技术的个人博客

0%

pyenv Python多环境管理神器

pyenv可在不同Python版本之间轻松切换,实现Python环境隔离,且支持自动激活和退出虚拟环境。

安装

Git Clone

这种方式Linux和MacOS都可通用

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

Linux

  1. 使用下列命令自动安装pyenv或者Git Clone的方式:
1
$ curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
  1. 配置环境变量
1
2
3
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile

或者,在.bashrc_profile直接写入

1
2
3
4
5
vim ~/.bash_profile
# pyenv
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
  1. 使用环境变量

    使用source命令重新执行上一步中修改的配置文件:

    1
    source ~/.bash_profile
  2. 验证

    1
    2
    [root@xxx ~]# pyenv -v
    pyenv 2.3.21-6-g7b713a88

Mac OS X

Git Clone方式配置环境变量

  • bash

    1
    2
    3
    4
    5
    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
    echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
    echo 'export PATH="$PYENV_ROOT/shims:$PATH"' >> ~/.bashrc
    echo 'eval "$(pyenv init -)"' >> ~/.bashrc
    exec $SHELL -l
  • zsh

    1
    2
    3
    4
    5
    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
    echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
    echo 'export PATH="$PYENV_ROOT/shims:$PATH"' >> ~/.zshrc
    echo 'eval "$(pyenv init -)"' >> ~/.zshrc
    exec $SHELL -l

Homebrew

  1. 安装

    1
    brew install pyenv
  1. 配置环境变量

    根据自身环境,将下方内容加到对应文件中: .bashrc / .zshrc

    1
    2
    3
    4
    export PYENV_ROOT=/usr/local/var/pyenv
    export PATH="$PYENV_ROOT/bin:$PATH"
    export PATH="$PYENV_ROOT/shims:$PATH"
    if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
  2. 验证

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    lvjing@bogon ~ % pyenv
    pyenv 2.3.18
    Usage: pyenv <command> [<args>]

    Some useful pyenv commands are:
    --version Display the version of pyenv
    activate Activate virtual environment
    commands List all available pyenv commands
    deactivate Deactivate virtual environment
    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-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

使用

基本使用

命令 描述
pyenv --version 查看pyenv的版本
pyenv versions 罗列当前已安装所有Python环境,如果是当前正在使用的环境,则前面会有个*
pyenv help 查看帮助
pyenv init 如果输入 pyenv 之后使用 tab 不补全,可以使用该命令进行初始即可使用补全命令
pyenv update 升级工具
brew upgrade pyenv Mac系统升级工具

安装环境

命令 描述
pyenv install -l 显示可以安装的版本列表
pyenv install 版本号 安装指定版本的Python
pyenv rehash 更新本地数据库,安装指定版本的 python 后使用

环境应用

命令 描述
pyenv global 版本号 更改本机版本,重启不会造成再次更改
pyenv local 版本号 会在当前目录创建 .python-version 文件,并记录设置的 python 环境,每次进入该目录会自动设置成该 python 环境
pyenv shell 版本号 更改当前 shell 下使用的 python 版本,临时生效,优先级高于 global

报错

  1. pyenv install 版本号执行,报错No module named '_lzma'

    • Mac安装

      brew install xz

    • Linux安装

      • Ubuntu:sudo apt-get install liblzma-dev

virtualenv插件

安装

  • 方式一:Git Clone

    1
    2
    cd .pyenv/plugins
    git clone https://github.com/pyenv/pyenv-virtualenv.git # 安装virtualenv插件
  • 方式二:Homebrew

    1
    brew install pyenv-virtualenv

配置

无论使用上述的哪种方式进行的安装,请根据自身环境,将下方内容加到对应文件中: .bashrc / .zshrc

1
eval "$(pyenv virtualenv-init -)"

使用

基本使用

命令 描述
pyenv virtualenv 3.8.3 env383 创建 3.8.3 版本虚拟环境
pyenv virtualenvs 显示环境
pyenv activate env383 激活使用指定的虚拟环境
pyenv deactivate 退出当前虚拟环境
pyenv uninstall env383 直接删除虚拟环境
rm -rf .pyenv/versions/3.8.3 删除版本环境
rm -rf .pyenv/versions/env383 删除虚拟环境

学习地址:http://www.tuohang.net/article/244919.html

报错

  1. 激活虚拟环境:pyenv activate env383,报错:

    1
    2
    Perhaps pyenv-virtualenv has not been loaded into your shell properly.
    Please restart current shell and try again.

    解决:

    1
    2
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"