Ubuntu安装Python3.6并切换到3.6版本「建议收藏」

Ubuntu安装Python3.6并切换到3.6版本「建议收藏」在此记录我在Ubuntu16.04系统上安装Python3.6并从Python2.7版本切换到3.6版本的过程。

大家好,又见面了,我是你们的朋友全栈君。

前言

在此记录我在Ubuntu 16.04 系统上安装Python3.6并从Python 2.7 版本切换到 3.6 版本的过程。

备注: 2022年4月15日14:39:40 基于评论区出现的问题,请不要轻易尝试博客方案,仅供参考。(因为时间过去太久了,我也忘记具体细节了。)建议用conda切换python环境会比较香。

补充

创建时间:2019年02月19日 00:59:21
修改时间:2019年9月4日16:21:40

在Ubuntu 14.04 上安装python3.6以及对应的pip,只需要如下指令:

# 先update一下
sudo apt-get update
sudo apt-get install software-properties-common python-software-properties -y 

# 这一句update好像不是必要的。
sudo apt-get update
sudo add-apt-repository ppa:jonathonf/python-3.6 -y

#这里必须update,不然无法安装python3.6
sudo apt-get update
sudo apt-get install python3.6 -y 

#没有curl的话,就install一下
sudo apt-get install curl -y 

# 安装pip
curl https://bootstrap.pypa.io/get-pip.py | sudo -H python3.6

这样既可,善。


1 了解自己系统上都有哪些Python版本

我的系统是 阿里云的Ubuntu 16.04 云服务器。

使用Python --version查询当前的Python版本,如下,Python当前版本为2.7:

dale@deheng:~$ python –version
Python 2.7.6

使用ls /usr/local/lib/ 查看本机上有哪些可用Python版本,如下,当前本地可用Python版本为2.7和3.4:

dale@deheng:~$ ls /usr/local/lib/
perl python2.7 python3.4

2 安装Python 3.6

sudo add-apt-repository ppa:jonathonf/python-3.6   #这个指令是真的方便。记住要按一下Enter键确认。
sudo apt-get update 
sudo apt-get install python3.6

这时候输入:ls /usr/local/lib/ 会发现出现了python3.6文件夹。

dale@deheng:~$ ls /usr/local/lib/
perl python2.7 python3.4 python3.6

参考[1].

3 将Python版本从2.7切换到3.6

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 3

#update-alternatives是ubuntu系统中专门维护系统命令链接符的工具,通过它可以很方便的设置系统默认使用哪个命令、哪个软件版本
# 上面三行指令最后的数字 1 2 3 分别代表优先级。1是最高。所以等下 config的时候,会发现默认版本是2.7(因为它的优先级设为了1).

而后输入sudo update-alternatives --config python(这是一个切换Python版本的指令),会出现如下选项:

dale@deheng:~$ sudo update-alternatives –install /usr/bin/python python /usr/bin/python3.6 3 dale@deheng:~$ sudo update-alternatives
–config python There are 4 choices for the alternative python (providing /usr/bin/python).>
Selection Path Priority Status
————————————————————

0 /usr/bin/python3.6 3 auto mode

  • 1 /usr/bin/python2.7 1 manual mode
    2 /usr/bin/python3.4 2 manual mode
    3 /usr/bin/python3.6 3 manual mode
    4 /usr/local/lib/python2.7 1 manual mode

Press enter to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/bin/python3.6 to provide
/usr/bin/python (python) in manual mode

如上,通过输入Python3.6对应的数字3,成功将Python版本设置成3.6。在此查看如下:

dale@deheng:~$ python
–version Python 3.6.3

参考[3]。

4 中间遇到的问题

4.1 问题一

在 第2节 运行sudo add-apt-repository ppa:jonathonf/python-3.6 的时候,如果出现add-apt-repository: command not found的情况,请使用如下命令:

sudo apt-get install software-properties-common python-software-properties 
#注释:即安装software-properties-common 和 python-software-properties 即可

参考[2]。

4.2 问题二

在 第3节 的操作中,遇到了一个问题,先描述、记录如下:

问题场景:
在第3节中,我先输入了sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1,然后输入python --version的时候,我发现我Python没了,提示要下载。

我不以为然,没去管这个问题,开始下载3.6,运行sudo add-apt-repository ppa:jonathonf/python-3.6,提示找不到add-apt-repository 这个command,然后我又sudo apt-get install software-properties-common python-software-properties,显示本机上有这两个软件了,但是报了如下错误:

dale@deheng:~$ sudo apt-get install software-properties-common
python-software-properties Reading package lists… Done Building
dependency tree Reading state information… Done
software-properties-common is already the newest version.
python-software-properties is already the newest version. 0 upgraded,
0 newly installed, 0 to remove and 173 not upgraded. 2 not fully
installed or removed. After this operation, 0 B of additional disk
space will be used. Do you want to continue? [Y/n] Y Setting up
python-pycurl (7.19.3-0ubuntu3) …
/var/lib/dpkg/info/python-pycurl.postinst: 6:
/var/lib/dpkg/info/python-pycurl.postinst: pycompile: Permission
denied dpkg: error processing package python-pycurl (–configure):
subprocess installed post-installation script returned error exit status 126 dpkg: dependency problems prevent configuration of python-software-properties: python-software-properties depends on python-pycurl; however: Package python-pycurl is not configured yet.

dpkg: error processing package python-software-properties
(–configure): dependency problems – leaving unconfigured Errors were encountered while processing: python-pycurl
python-software-properties E: Sub-process /usr/bin/dpkg returned an error code (1)
在这里插入图片描述

解决方案: 想了很久,发现是自己的sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1导致当前Python版本 不见了,这样的话等于Ubuntu的很多功能就用不了了。我随后利用本机已有的3.4和2.7版本,进行配置,以找回Python版本:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2
sudo update-alternatives --config python

然后python --version,发现Python版本回归了。这时候又可以正常下载Python3.6了,这个错误也没了。

总结

写博客还是耗时间啊。

参考文献

主要:
[1] Ubuntu16.04怎样安装Python3.6 https://www.cnblogs.com/yjlch1016/p/8641910.html
[2] Ubuntu的add-apt-repository: command not found https://blog.csdn.net/dogfish/article/details/67150703
[3] Ubuntu16.04怎样安装Python3.6 https://www.cnblogs.com/yjlch1016/p/8641910.html

次要:
[4] Ubuntu环境下python2和python3切换 https://blog.csdn.net/qq_18815817/article/details/78874808
[5] Ubuntu下Python2与Python3的共存配置 https://blog.csdn.net/weixin_40293491/article/details/81183491
[6] 在Ubuntu中通过update-alternatives切换软件版本 https://persevere.iteye.com/blog/1479524

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/152423.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • 成功的测试通常是运行测试用例后_成功的测试是指运行测试用例后

    成功的测试通常是运行测试用例后_成功的测试是指运行测试用例后前言用过pytest的小伙伴都知道,pytest的运行方式是非常丰富的,可以说是你想怎么运行怎么运行,想运行哪些运行哪些,那httprunner是否同样可以呢?运行用例的各种方式运行指定路径的用

    2022年7月28日
  • Error running’ xxxxxx’: Command line is too long. Shorten command line for xxxxxxxxx

    Error running’ xxxxxx’: Command line is too long. Shorten command line for xxxxxxxxx在IntellIDEA运行main函数的时候遇到了如下错误:Errorrunning’xxxxxx’:Commandlineistoolong.Shortencommandlineforxxxxxxxxx后来从网上查找资料发现是命令行的参数太长,需要进行修改。修改过程如下:在run->editconfiguration中修改红色画圈部分,选择如下…

    2022年8月22日
  • 详解stacking过程

    翻到之前自己写的这篇博客,感觉写的还是不够简洁明了,特地回来改一下,顺便文末附上Kaggle内相关操作的代码,希望能够帮助学习的同学能够瞬间理解stacking这个概念。stacking:stacking是一种分层模型集成框架。以两层为例,第一层由多个基学习器组成,其输入为原始训练集,第二层的模型则是以第一层基学习器的输出作为特征加入训练集进行再训练,从而得到完整的stacking模型。sta…

    2022年4月6日
  • java获取服务器文件路径,干货满满!

    java获取服务器文件路径,干货满满!一、SpringCloud微服务概念定义提起微服务,不得不提SpringCloud全家桶系列,SpringCloud是一个服务治理平台,是若干个框架的集合,提供了全套的分布式系统解决方案。包含了:服务注册与发现、配置中心、服务网关、智能路由、负载均衡、断路器、监控跟踪、分布式消息队列等等。SpringCloud通过SpringBoot风格的封装,屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、容易部署的分布式系统开发工具包。开发者可以快速的启动服务或构建应用、同时能够

    2022年7月11日
  • Effective C++ Item 40 明智而审慎地使用多重继承

    Effective C++ Item 40 明智而审慎地使用多重继承

    2021年12月6日
  • pycharm linux激活码-激活码分享

    (pycharm linux激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~AFH5K5BM31-eyJsaWNlb…

    2022年3月30日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号