文章目錄
前言
==補(bǔ)充==
1 了解自己系統(tǒng)上都有哪些Python版本
2 安裝Python 3.6
3 將Python版本從2.7切換到3.6
4 中間遇到的問題
4.1 問題一
4.2 問題二
總結(jié)
參考文獻(xiàn)
前言
在此記錄我在Ubuntu 16.04 系統(tǒng)上安裝Python3.6并從Python 2.7 版本切換到 3.6 版本的過程矫限。
補(bǔ)充
創(chuàng)建時(shí)間:2019年02月19日 00:59:21
修改時(shí)間:2019年9月4日16:21:40
在Ubuntu 14.04 上安裝python3.6以及對應(yīng)的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 了解自己系統(tǒng)上都有哪些Python版本
我的系統(tǒng)是 阿里云的Ubuntu 16.04 云服務(wù)器假哎。
使用Python --version查詢當(dāng)前的Python版本,如下歉嗓,Python當(dāng)前版本為2.7:
dale@deheng:~$ python --version
Python 2.7.6
使用ls /usr/local/lib/ 查看本機(jī)上有哪些可用Python版本丰介,如下,當(dāng)前本地可用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? #這個(gè)指令是真的方便鉴分。記住要按一下Enter鍵確認(rèn)哮幢。
sudo apt-get update
sudo apt-get install python3.6
1
2
3
這時(shí)候輸入:ls /usr/local/lib/ 會發(fā)現(xiàn)出現(xiàn)了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系統(tǒng)中專門維護(hù)系統(tǒng)命令鏈接符的工具志珍,通過它可以很方便的設(shè)置系統(tǒng)默認(rèn)使用哪個(gè)命令橙垢、哪個(gè)軟件版本
# 上面三行指令最后的數(shù)字 1 2 3 分別代表優(yōu)先級。1是最高伦糯。所以等下 config的時(shí)候柜某,會發(fā)現(xiàn)默認(rèn)版本是2.7(因?yàn)樗膬?yōu)先級設(shè)為了1).
1
2
3
4
5
6
而后輸入sudo update-alternatives --config python(這是一個(gè)切換Python版本的指令),會出現(xiàn)如下選項(xiàng):
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對應(yīng)的數(shù)字3喂击,成功將Python版本設(shè)置成3.6。在此查看如下:
dale@deheng:~$ python
–version Python 3.6.3
參考[3]淤翔。
4 中間遇到的問題
4.1 問題一
在 第2節(jié) 運(yùn)行sudo add-apt-repository ppa:jonathonf/python-3.6 的時(shí)候翰绊,如果出現(xiàn)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節(jié) 的操作中监嗜,遇到了一個(gè)問題琳要,先描述、記錄如下:
問題場景:
在第3節(jié)中秤茅,我先輸入了sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1,然后輸入python --version的時(shí)候童叠,我發(fā)現(xiàn)我Python沒了框喳,提示要下載。
我不以為然厦坛,沒去管這個(gè)問題五垮,開始下載3.6,運(yùn)行sudo add-apt-repository ppa:jonathonf/python-3.6杜秸,提示找不到add-apt-repository 這個(gè)command放仗,然后我又sudo apt-get install software-properties-common python-software-properties,顯示本機(jī)上有這兩個(gè)軟件了撬碟,但是報(bào)了如下錯(cuò)誤:
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)
解決方案: 想了很久诞挨,發(fā)現(xiàn)是自己的sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1導(dǎo)致當(dāng)前Python版本 不見了,這樣的話等于Ubuntu的很多功能就用不了了呢蛤。我隨后利用本機(jī)已有的3.4和2.7版本惶傻,進(jìn)行配置,以找回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其障,發(fā)現(xiàn)Python版本回歸了银室。這時(shí)候又可以正常下載Python3.6了,這個(gè)錯(cuò)誤也沒了励翼。
總結(jié)
寫博客還是耗時(shí)間啊蜈敢。
參考文獻(xiàn)
主要:
[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環(huán)境下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
# 官網(wǎng)下載安裝包
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
# 創(chuàng)建軟連接,先查看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