更改Ubuntu默认python版本的两种方法python-> Anaconda
作者:Alanyannick 时间:2021-07-30 15:33:28
你可以按照以下方法使用 ls 命令来查看你的系统中都有那些 Python 的二进制文件可供使用。
$ ls /usr/bin/python*
/usr/bin/python /usr/bin/python2 /usr/bin/python2.7 /usr/bin/python3 /usr/bin/python3.4 /usr/bin/python3.4m /usr/bin/python3m
执行如下命令查看默认的 Python 版本信息:
$ python --version
Python 2.7.8
1、基于用户修改 Python 版本:
想要为某个特定用户修改 Python 版本,只需要在其 home 目录下创建一个 alias(别名) 即可。打开该用户的 ~/.bashrc文件,添加新的别名信息来修改默认使用的 Python 版本。
alias python='/usr/bin/python3.4'
一旦完成以上操作,重新登录或者重新加载 .bashrc 文件,使操作生效。
$ . ~/.bashrc
检查当前的 Python 版本。
$ python --version
Python 3.4.2
2、 在系统级修改 Python 版本
我们可以使用 update-alternatives 来为整个系统更改 Python 版本。以 root 身份登录,首先罗列出所有可用的 python 替代版本信息:
# update-alternatives --list python
update-alternatives: error: no alternatives for python
如果出现以上所示的错误信息,则表示 Python 的替代版本尚未被 update-alternatives 命令识别。想解决这个问题,我们需要更新一下替代列表,将 python2.7 和 python3.4 放入其中。
# 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.4 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.4.2
接下来,我们再次列出可用的 Python 替代版本。
# update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.4
现在开始,我们就可以使用下方的命令随时在列出的 Python 替代版本中任意切换了。
# update-alternatives --config python
# python --version
Python 2.7.8
3、移除替代版本
一旦我们的系统中不再存在某个 Python 的替代版本时,我们可以将其从 update-alternatives 列表中删除掉。例如,我们可以将列表中的 python2.7 版本移除掉。
# update-alternatives --remove python /usr/bin/python2.7
update-alternatives: removing manually selected alternative - switching python to auto mode
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode
方法2、移除软连接
rm -rf /data/logs
ln -s /temp/logs /data/logs
解决软连接ln报错-bash: /usr/local/bin/mysql: Too many levels of symbolic links
总结
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python实现Decorator模式实例代码
python使用正则表达式检测密码强度源码分享
python工具之清理 Markdown 中没有引用的图片
![](https://img.aspxhome.com/file/2023/0/67300_0s.png)
python 根据正则表达式提取指定的内容实例详解
互联网产品设计零碎记
![](https://img.aspxhome.com/file/UploadPic/20109/25/diedai-600x64-87s.png)
Numpy中Meshgrid函数基本用法及2种应用场景
![](https://img.aspxhome.com/file/2023/3/101203_0s.jpg)
浅谈python中的面向对象和类的基本语法
![](https://img.aspxhome.com/file/2023/4/59644_0s.jpg)
当设计师遭遇HTML5
Python3中的f-Strings增强版字符串格式化方法
Python地理地图可视化folium标记点弹窗设置代码(推荐)
![](https://img.aspxhome.com/file/2023/1/70871_0s.jpg)
分析Python的Django框架的运行方式及处理流程
![](https://img.aspxhome.com/file/2023/9/93759_0s.png)
使用python实现链表操作
![](https://img.aspxhome.com/file/2023/4/80604_0s.png)
Python自动化之数据驱动让你的脚本简洁10倍【推荐】
![](https://img.aspxhome.com/file/2023/2/67892_0s.jpg)
arcgis使用Python脚本进行批量截图功能实现
![](https://img.aspxhome.com/file/2023/7/95777_0s.jpg)
Python中__init__和__new__的区别详解
解决python中的幂函数、指数函数问题
Mysql的服务无法启动的1067错误解决
python2.7读取文件夹下所有文件名称及内容的方法
Python爬取成语接龙类网站
![](https://img.aspxhome.com/file/2023/5/87795_0s.png)