Ubuntu安装Python3.6并切换到3.6版本

文章目录

前言

==补充==

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

2 安装Python 3.6

3 将Python版本从2.7切换到3.6

4 中间遇到的问题

4.1 问题一

4.2 问题二

总结

参考文献

前言

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

补充

创建时间: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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

这样既可,善。

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

1

2

3

这时候输入: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).

1

2

3

4

5

6

而后输入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 即可

1

2

参考[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

1

2

3

然后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


Ubuntu16.04下安装python3.6

# 官网下载安装包

1. wget http://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz

# 解压

2. tar -xvzf Python-3.6.4.tgz

# 安装

3. cd Python-3.6.4

4. ./configure --with-ssl

# 编译

5. sudo make

6. sudo make install

# 创建软连接,先查看python3.6.4的安装路径

7. which python3    # /usr/local/bin/python38.mv python python_org  ##保留原来的python

9. sudo  ln -s /usr/local/bin/python3 python10.pip3 install --upgrade pip ###更新pip311.mv pip pip_org  ##保留原来的pip

12. sudo  ln -s /usr/local/bin/pip3 pip

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 193,968评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,682评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,254评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,074评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,964评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,055评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,484评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,170评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,433评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,512评论 2 308
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,296评论 1 325
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,184评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,545评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,150评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,437评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,630评论 2 335