pyenv - Python Version Management
安装方式一
1 2 3 4 5 6 7 8 9 10 11
| $ curl https://pyenv.run | bash
export PYENV_ROOT="$HOME/.pyenv" [[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"
|
安装方式二
1 2 3
| $ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
$ cd ~/.pyenv && src/configure && make -C src
|
安装必要的包(用于构建Python)
1 2 3
| $ sudo apt install build-essential libssl-dev zlib1g-dev libbz2-dev \ libreadline-dev libsqlite3-dev curl libncursesw5-dev xz-utils tk-dev \ libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
|
pyenv
使用
1 2 3 4
| $ pyenv install 3.12.1
$ pyenv global/local/shell $ pyenv version/versions
|
1 2 3 4 5 6 7 8 9
| $ git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
$ pyenv virtualenvs $ pyenv virtualenv $ pyenv activate $ pyenv deactivate $ pyenv virtualenv-delete
|
1 2 3 4 5 6
| $ git clone https://github.com/pyenv/pyenv-virtualenvwrapper.git $(pyenv root)/plugins/pyenv-virtualenvwrapper
$ pyenv virtualenvwrapper $ pyenv virtualenvwrapper_lazy
|
使用pyvenv
而不是virtualenv
:
1
| export PYENV_VIRTUALENVWRAPPER_PREFER_PYVENV="true"
|
virtualenvwrapper文档。virtualenvwrapper
可以提供mkvirtualenv
,lsvirtualenv
,workon
这样的命令,以方便使用虚拟环境。
一点说明
一般一个系统上只安装一个版本的Python
。有了pyenv
就可以方便地安装多个版本,并在不同版本间切换。
早期的Python只能装一组包(一个环境),难免会出现包版本冲突的情况。于是发展出了virtualenv
这的方案,它可以创建多个虚拟环境,这样就可以在不同的环境中安装不同版本的包。
在virtualenv
的基础上,它的一个子集成为了后来Python内置库——venv
模块(pyvenv
包)。
virtualenvwrapper
提供了方便使用虚拟环境的命令。