Ubuntu18.04下python版本完美切换的解决方法
作者:一杯南风 时间:2021-08-22 11:24:19
ubuntu18.04版本,python版本python2.7,python3.5,python3.6
因为安装一些库会安装到python3.6上,而默认使用的是python2.7,使用python3,默认会使用python3.5,无法调用安装包。
解决方法:
一、使用python xx.py运行程序时,加上版本号。比如python3.6 xx.py
二、1.要以root身份操作
yz@yz-pc:~$ sudo su
2.确认本机下的python默认版本。调出终端,输入python即可查看默认的版本:
root@yz-pc:/home/yz# python
Python 3.6.5 (default, Apr 1 2018, 05:46:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
root@yz-pc:/home/yz# python2.7
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
root@yz-pc:/home/yz# python3
Python 3.6.5 (default, Apr 1 2018, 05:46:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
root@yz-pc:/home/yz# python3.5
3.如何切换这两个版本以及切换默认的python版本:
我们可以使用 update-alternatives 来为整个系统更改Python 版本。以 root 身份登录,首先罗列出所有可用的python 替代版本信息:
#update-alternatives --list python
update-alternatives: error: no alternatives for python
如果出现以上所示的错误信息,则表示 Python 的替代版本尚未被update-alternatives 命令识别。想解决这个问题,我们需要更新一下替代列表,将python2.7 和 python3.6放入其中。
# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
# update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode
--install 选项使用了多个参数用于创建符号链接。最后一个参数指定了此选项的优先级,如果我们没有手动来设置替代选项,那么具有最高优先 级的选项就会被选中。这个例子中,我们为/usr/bin/python3.4 设置的优先级为2,所以update-alternatives 命 令会自动将它设置为默认 Python 版本。
# python --version
Python 3.5.2
接下来,我们再次列出可用的 Python 替代版本。
# update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.5
现在开始,我们就可以使用下方的命令随时在列出的 Python 替代版本中任意切换了。
(这一步是最关键的)
# update-alternatives --config python
下面就简单了,会提示你输入序号,你想用哪个版本为默认,就输入序号就可以了!
结束!
参考文章:ubuntu 16.04下python版本切换的方法
来源:https://blog.csdn.net/qq_34343669/article/details/82888992
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 输入一个数n,求n个数求乘或求和的实例
Python decorator拦截器代码实例解析
django时区问题的解决
Python中ttkbootstrap的介绍与基本使用
![](https://img.aspxhome.com/file/2023/9/91999_0s.gif)
PHP 截取字符串 分别适合GB2312和UTF8编码情况
在Python的Flask中使用WTForms表单框架的基础教程
![](https://img.aspxhome.com/file/2023/6/90746_0s.png)
Python实例一个类背后发生了什么
python 实现两个npy档案合并
Django实现文件上传和下载功能
![](https://img.aspxhome.com/file/2023/2/85012_0s.jpg)
Oracle 日期的一些简单使用
Python可视化Matplotlib折线图plot用法详解
![](https://img.aspxhome.com/file/2023/5/99565_0s.png)
python开发之文件操作用法实例
使用 OpenCV-Python 识别答题卡判卷功能
![](https://img.aspxhome.com/file/2023/5/87515_0s.jpg)
python实现求纯色彩图像的边框
![](https://img.aspxhome.com/file/2023/4/109974_0s.jpg)
使用Python从零开始撸一个区块链
![](https://img.aspxhome.com/file/2023/7/66827_0s.jpg)
Python 12306抢火车票脚本
Python实现获取照片的地理定位信息
![](https://img.aspxhome.com/file/2023/8/72788_0s.png)