Ubuntu16.04 中默认安装的是 Python2.7 和 Python3.5 版本,但在实际使用中有项目要求 Python3.6 或 Python3.7 等版本,因此,有必要同时安装使用多个 Python 版本.
1. 安装
[1] - 配置软件仓库
# 添加第三方库
sudo add-apt-repository ppa:jonathonf/python-3.6
sudo add-apt-repository ppa:jonathonf/python-3.7
# 或
sudo add-apt-repository ppa:deadsnakes/ppa
# 清除 PPA
sudo apt install ppa-purge
sudo ppa-purge ppa:deadsnakes/ppa
[2] - 安装 Python3.6 或 Python3.7
sudo apt-get update
sudo apt-get install python3.6
sudo apt-get install python3.6-dev
sudo apt-get install python3.7
sudo apt-get install python3.7-dev
[3] - 查看 Python 版本信息
which python3
# /usr/bin/python3
which python3.6
# /usr/bin/python3.6
which python3.7
# /usr/bin/python3.7
python -V
# Python 2.7.12
python3 -V
# Python 3.5.2
python3.6 -V
# Python 3.6.7
python3.7 -V
# Python 3.7.1
[4] - 设置 Python3 默认 Python 版本:
# 调整 Python3 的优先级,使得 Python3.6 优先级较高
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 3
sudo update-alternatives --config python3
python3 -V
# Python 3.6.7
2. 问题解决
[1] - 升级到python3.6, 会导致python库的引用产生混乱.
# 拷贝python3.5的apt-pkg*.so 名重名为python3.6的apt-pkg*.so
cd /usr/lib/python3/dist-packages/
sudo cp apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.cpython-36m-x86_64-linux-gnu.so
[2] - 安装 pip 时出现错误:ModuleNotFoundError: No module named 'distutils.util'
.
sudo apt-get install python3-distutils
[3] - pip 使用时出现错误:No module named apt_pkg
:
sudo apt-get remove --purge python3-apt
sudo apt-get install python3-apt
[4] - pip 使用时出现错误:pip unsupported local setting
:
export LC_ALL=C
3. apt-add-repository 报错
执行:
sudo add-apt-repository ppa:deadsnakes/ppa
报错:
add-apt-repository:command not found
解决方案:
sudo apt-get install python-software-properties
sudo apt-get install software-properties-common
#再重新执行
sudo add-apt-repository ppa:deadsnakes/ppa