以前一直是通过修改/bin/目录下的软连接来实现python版本的切换的,十分不方便,而且安装新的python版本也十分繁琐,尝试了一把pyenv,瞬间感觉真香,记录下安装过程备忘。
这一步必须,否则python编译会报警告或异常,导致某些扩展无法使用,例如:
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?
安装命令:
# sudo apt install libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev libffi-dev \
libncurses5-dev libncursesw5-dev liblzma-dev
下载
# git clone https://github.com/yyuu/pyenv.git ~/.pyenv
增加以下内容到~/.bashrc
export PATH=~/.pyenv/bin:$PATH
export PYENV_ROOT=~/.pyenv
eval "$(pyenv init -)"
更新环境变量
# source ~/.bashrc
# pyenv install 3.8.5
Downloading Python-3.8.5.tar.xz...
-> https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tar.xz
pyenv下载可能很慢,可以拷贝显示的下载链接,在浏览器中下载,然后放到~/.pyenv/cache/
目录下(cache
目录需要手动创建),再重新执行pyenv install 3.8.5
# pyenv versions
* system (set by /home/zpzhou/.pyenv/version)
3.8.5
可使用命令pyenv global <version>
来切换。
切换前:
# python -V
Python 2.7.13
切换:
# pyenv global 3.8.5
切换后:
# python -V
Python 3.8.5