1、 使用apt安装要apt update
本文记录在 Ubuntu 16.04 上将 python 升级为 3.8 版本,并配置为系统默认 python3 的过程。
在 Ubuntu 16.04 中,python3 的默认版本为 3.5:
$ python3 -V
Python 3.5.2
本文以在 Ubuntu 16.04 中安装为例,方法同样适用于 Ubuntu 18.04 。
Ubuntu 官方 apt 库中还未收录 python 3.8,这里使用 deadsnakes PPA 库安装。
sudo apt update 过程中出现的问题
https://blog.csdn.net/zml194849/article/details/121850581
2、apt安装python3.8
1)安装依赖包
添加deadsnakes PPA源
sudo add-apt-repository ppa:deadsnakes/ppa
Press [ENTER] to continue or Ctrl-c to cancel adding it.
2)安装python3.8
$ sudo apt install python3.8
$ python3.8 -V
Python 3.8.2
3、pip安装python
修改pip版本
Ubuntu系统内置python2.7,如果你安装pip使用
sudo apt-get install python-pip
那么,你仅仅是在内置的python2.7里面安装了pip,你下载的python3 在调用pip的时候会提醒你can’t open file ‘pip’。所以我们需要在python3里面安装pip
sudo apt-get install python3-pip
以后使用python3安装包时都用pip3 install xxxx
的方式
更改默认版本为Python3之后,sudo apt-get install python-pip
安装的pip在python3里面。
注
:千万不可图省事将内置python2卸载,这有可能造成系统某些功能不能正常使用
4、更改默认版本
1)我们可以使用 update-alternatives 来为整个系统更改Python 版本。以 root 身份登录,首先罗列出所有可用的python 替代版本信息:
$ update-alternatives --list python
update-alternatives: error: no alternatives for python
- 如果出现以上所示的错误信息,则表示 Python 的替代版本尚未被update-alternatives 命令识别。想解决这个问题,我们需要更新一下替代列表,将python2.7、python3.5 和 python3.8 放入其中。
将 python 各版本添加到 update-alternatives
$ which python3.8
/usr/bin/python3.8
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 3
$ which python3.5
/usr/bin/python3.5
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 2
$ sudo update-alternatives --install /usr/bin/python2 python2 /usr/bin/python2.7 1
–install 选项使用了多个参数用于创建符号链接。最后一个参数指定了此选项的优先级,如果我们没有手动来设置替代选项,那么具有最高优先 级的选项就会被选中。这个例子中,我们为/usr/bin/python3.5 设置的优先级为2,所以update-alternatives 命 令会自动将它设置为默认 Python 版本。
3) 再次列出可用的python 替代版本
$ update-alternatives --list python
/usr/bin/python3.8
/usr/bin/python3.5
/usr/bin/python2.7
- 在列出的 Python 替代版本中任意切换,配置 python3 默认指向 python3.8
$ sudo update-alternatives --config python
There are 3 choices for the alternative python3 (providing /usr/bin/python).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/python3.8 3 自动模式
1 /usr/bin/python2.7 1 手动模式
2 /usr/bin/python3.5 2 手动模式
3 /usr/bin/python3.8 3 手动模式
#要维持当前值[*]请按<回车键>,或者键入选择的编号:
Press to keep the current choice[*], or type selection number: 3
选择/输入3, 回车。
配置python 默认指向python3.8
5、测试 python 版本
$ python3 -V
Python 3.8.2