在CentOS7下,預設安裝的就是python2.7,我現在來教大家如何安裝python3:
1、先安裝python3.6可能使用的依賴
# yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel zlib-devel
2、下載python原始碼包,位址為https://www.python.org/downloads/source/,然後透過xftp上傳上去,或直接使用wget指令,wget https://www.python.org/ ftp/python/3.6.1/Python-3.6.1.tgz下載到本機
# tar -zxvf Python-3.6.1.tgz # cd Python-3.6.1/
3、把python3.6安裝到/usr/local目錄下
# ./configure --prefix=/usr/local/ # make # make altinstall
4、更改/usr/bin/python連結
# cd /usr/bin # mv python python.backup # ln -s /usr/local/bin/python3.6 /usr/bin/python # ln -s /usr/local/bin/python3.6 /usr/bin/python3 # rm -rf /usr/bin/python2 # ln -s /usr/bin/python2.7 /usr/bin/python2
5、更改yum腳本的python依賴
# cd /usr/bin # ll yum*
有以下檔案:
-rwxr-xr-x 1 root root 802 Jan 2 17:53 yum -rwxr-xr-x 1 root root 9374 Jan 2 17:53 yum-builddep -rwxr-xr-x 1 root root 8091 Jan 2 17:53 yum-config-manager -rwxr-xr-x 1 root root 7610 Jan 2 17:53 yum-debug-dump -rwxr-xr-x 1 root root 7904 Jan 2 17:54 yum-debug-restore -rwxr-xr-x 1 root root 10906 Jan 2 17:54 yumdownloader -rwxr-xr-x 1 root root 11032 Jan 2 17:54 yum-groups-manager
修改上述檔案頭:#!/usr/bin/python 改為 #!/usr/bin/python2
可以安裝一個tree測試一下
# yum -y install tree
如果出現以下報錯:File “/usr/libexec/urlgrabber-ext-down”, line 28
vim /usr/libexec/urlgrabber-ext-down
將/usr/bin/python改為/usr/bin/python2即可
這樣,yum安裝軟體的時候就不會報錯了,而且預設安裝的就是python3了,輸入python時,就會進入python3介面,如果想進入python2,那麼就輸入python2好了。
以上是在Linux CentOS7 下安裝 python3的詳細內容。更多資訊請關注PHP中文網其他相關文章!