溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

CentOS7 安裝 Python3.6

發(fā)布時間:2020-07-06 11:29:11 來源:網(wǎng)絡(luò) 閱讀:4385 作者:騎士救兵 欄目:編程語言

先看編譯安裝,編譯安裝后的文件目錄基本和Windows平臺上目錄差不多。指定安裝路徑的話,所有的文件都是在一起的。
后面有yum的安裝的方法。

編譯安裝

編譯安裝,會裝好setuptools和pip這兩個工具,都在site-packages這個文件夾里。之后再用pip安裝各種模塊,也都在pip所在的site-packages文件夾里??傊械臇|西都在python3的安裝目錄下。

下載安裝包

[root@Python36 ~]# cd ~
[root@Python36 ~]# mkdir download
[root@Python36 ~]# cd download/
[root@Python36 download]# wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
--2018-09-29 15:49:52--  https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
正在解析主機 www.python.org (www.python.org)... 151.101.108.223, 2a04:4e42:36::223
正在連接 www.python.org (www.python.org)|151.101.108.223|:443... 已連接。
已發(fā)出 HTTP 請求,正在等待回應(yīng)... 200 OK
長度:22930752 (22M) [application/octet-stream]
正在保存至: “Python-3.6.6.tgz”

100%[==================================================================>] 22,930,752  59.5KB/s 用時 7m 48s 

2018-09-29 15:57:40 (47.9 KB/s) - 已保存 “Python-3.6.6.tgz” [22930752/22930752])

[root@Python36 download]#

下載后解壓:

[root@Python36 download]# tar -zxvf Python-3.6.6.tgz

安裝依賴包

我一開始并沒有裝下面的依賴包,而是直接編譯安裝,等報錯了再找解決辦法:

[root@Python36 ~]# yum install gcc zlib-devel

.install 的時候需要gcc
make install 的時候需要zlib-devel

編譯安裝

如果缺少依賴包,編輯的時候就會有問題。到時候就查看錯誤信息,到網(wǎng)上搜一下,看看是缺少了哪個依賴包:

[root@Python36 download]# cd Python-3.6.6
[root@Python36 Python-3.6.6]# ./configure prefix=/usr/local/python3

注意,這里的編譯命令有點問題,還需要加一個參數(shù)--with-ssl。否則python可以順利安裝并且正常使用,但是運行pip的時候會有問題。具體在這節(jié)編譯安裝的最后加了一個小節(jié)pip3報錯進行說明。
這里可以就按上面的命令來,然后參考最后一小節(jié)的方法來解決。不過既然已經(jīng)知道有問題了,就把參數(shù)加上。把上面的命令替換為這個,多加一個參數(shù):

yum install openssl-devel
./configure prefix=/usr/local/python3 --with-ssl

在編譯安裝python3.6.8的時候,需要在編譯之前先把 openssl-devel 這個包也安裝好。否則加了參數(shù)也是沒用的

指定安裝目錄是:/usr/local/python3。
如果不指定安裝目錄的話,安裝后可執(zhí)行文件默認放在/usr/local/bin(默認放這里的可執(zhí)行文件后面就不用另外再創(chuàng)建軟鏈接了),庫文件默認放在/usr/local/lib,配置文件默認放在/usr/local/etc,其它的資源文件放在/usr/local/share,比較凌亂。似乎也不是太亂的樣子,不過網(wǎng)上的文章都推薦在默認位置下再建一級目錄,全部放一起,就是這里的做法。
我們指定安裝目錄,以后卸載的話直接刪除目錄就可以干凈卸載了。
然后make:

[root@Python36 Python-3.6.6]# make

這里遇到報錯了:

gcc -pthread   -Xlinker -export-dynamic -o Programs/_testembed Programs/_testembed.o libpython3.6m.a -lpthread -ldl  -lutil   -lm  
# Substitution happens here, as the completely-expanded BINDIR
# is not available in configure
sed -e "s,@EXENAME@,/usr/local/python3/bin/python3.6m," < ./Misc/python-config.in >python-config.py
# Replace makefile compat. variable references with shell script compat. ones;  -> 
LC_ALL=C sed -e 's,\$(\([A-Za-z0-9_]*\)),\$\{\1\},g' < Misc/python-config.sh >python-config
# On Darwin, always use the python version of the script, the shell
# version doesn't use the compiler customizations that are provided
# in python (_osx_support.py).
if test `uname -s` = Darwin; then \
        cp python-config.py python-config; \
fi
[root@Python36 Python-3.6.6]#

直接根據(jù)提示運行上面給的命令,然后再make:

[root@Python36 Python-3.6.6]# sed -e "s,@EXENAME@,/usr/local/python3/bin/python3.6m," < ./Misc/python-config.in >python-config.py
[root@Python36 Python-3.6.6]# make
 CC='gcc -pthread' LDSHARED='gcc -pthread -shared  ' OPT='-DNDEBUG -g -fwrapv -O3 -Wall'        _TCLTK_INCLUDES='' _TCLTK_LIBS=''   ./python -E ./setup.py  build
running build
running build_ext
INFO: Can't locate Tcl/Tk libs and/or headers
warning: building with the bundled copy of libffi is deprecated on this platform.  It will not be distributed with Python 3.7

Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2                  _curses               _curses_panel      
_dbm                  _gdbm                 _lzma              
_sqlite3              _ssl                  _tkinter           
readline              zlib                                     
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
atexit                pwd                   time               
running build_scripts
copying and adjusting /root/download/Python-3.6.6/Tools/scripts/pydoc3 -> build/scripts-3.6
copying and adjusting /root/download/Python-3.6.6/Tools/scripts/idle3 -> build/scripts-3.6
copying and adjusting /root/download/Python-3.6.6/Tools/scripts/2to3 -> build/scripts-3.6
copying and adjusting /root/download/Python-3.6.6/Tools/scripts/pyvenv -> build/scripts-3.6
changing mode of build/scripts-3.6/pydoc3 from 644 to 755
changing mode of build/scripts-3.6/idle3 from 644 to 755
changing mode of build/scripts-3.6/2to3 from 644 to 755
changing mode of build/scripts-3.6/pyvenv from 644 to 755
renaming build/scripts-3.6/pydoc3 to build/scripts-3.6/pydoc3.6
renaming build/scripts-3.6/idle3 to build/scripts-3.6/idle3.6
renaming build/scripts-3.6/2to3 to build/scripts-3.6/2to3-3.6
renaming build/scripts-3.6/pyvenv to build/scripts-3.6/pyvenv-3.6
[root@Python36 Python-3.6.6]# 

make 成功后就是 make install :

[root@Python36 Python-3.6.6]# make install

驗證

直接先運行python3,再確認一下版本信息:

[root@Python36 ~]# cd /usr/local/python3/
[root@Python36 python3]# ./bin/python3
Python 3.6.6 (default, Sep 29 2018, 16:19:00) 
[GCC ④.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@Python36 python3]# 

退出用 exit()

創(chuàng)建軟鏈接

創(chuàng)建軟鏈接的方法不是很好,還可以通過修改環(huán)境變量PATH來解決。不過還是先看下如何通過創(chuàng)建軟鏈接解決問題。
直接執(zhí)行python3命令是找不到的,在python3的啟動命令加一條軟鏈接到系統(tǒng)目錄:

[root@Python36 ~]# python3
-bash: python3: 未找到命令
[root@Python36 ~]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@Python36 ~]# python3
Python 3.6.6 (default, Sep 29 2018, 16:19:00) 
[GCC ④.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

pip工具是安裝各種python包的,也很常用,也加個軟鏈接:

[root@Python36 ~]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
[root@Python36 ~]# pip3 --version
pip 10.0.1 from /usr/local/python3/lib/python3.6/site-packages/pip (python 3.6)

修改環(huán)境變量PATH

上面軟鏈接,一次只解決了一個命令的問題。其實/usr/local/python3/bin/目錄下所有的文件都是可執(zhí)行文件,都需要設(shè)置為可以執(zhí)行。當(dāng)然也可以批量創(chuàng)建軟鏈接,把 /usr/local/python3/bin/目錄下的所有文件都創(chuàng)建一個軟鏈接。但是這么做一方面很煩,而且解決不了將來的問題。因為將來使用pip安裝時生成的可執(zhí)行文件都會放到 /usr/local/python3/bin/目錄中,到時候還是需要再創(chuàng)建軟鏈接。

產(chǎn)生問題的原因
會出現(xiàn)這個問題是因為安裝的時候我們手動指定了安裝目錄,原本這些文件都是在/usr/local/bin/目錄下的,而這個目錄里的文件都是可以直接執(zhí)行的。
系統(tǒng)的環(huán)境變量PATH,指定了哪些目錄用來存放可執(zhí)行文件,這些目錄都的文件都是在全局可以直接調(diào)用執(zhí)行的。查看變量值的方法如下:

[root@Python36 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@Python36 ~]# 

設(shè)置環(huán)境變量
這里在profile中設(shè)置PATH,修改全局的環(huán)境變量。
編輯/etc/profile文件,找到export行,在下面新增加一行:

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCOntrOL
export PATH=$PATH:/usr/local/python3/bin

第一行是文件中原本的內(nèi)容,第二行是我們增加的內(nèi)容。
修改不會立刻生效,如果需要立即生效的話,可以執(zhí)行source命令:

[root@Python36 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@Python36 ~]# source /etc/profile
[root@Python36 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/python3/bin
[root@Python36 ~]# 

用python命令進入python3

還可以把默認的python的軟鏈接也指向python3,就是把系統(tǒng)默認的用python命令啟動python2給替換成啟動python3。這樣做的問題是,系統(tǒng)的yum使用要python2,因此替換為python3后,yum就無法正常工作了。
解決辦法就是修改yum的配置文件 vi /usr/bin/yum ,然后找到開頭的 #!/usr/bin/python 修改為 #!/usr/bin/python2.7 保存退出就好了。

pip3報錯

按照這里上面介紹的方法安裝完成后,在使用pip的時候會出現(xiàn)報錯:

[root@Python36 ~]# pip3 install -i https://mirrors.163.com/pypi/simple ansible
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Looking in indexes: https://mirrors.163.com/pypi/simple
Collecting ansible
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /pypi/simple/ansible/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /pypi/simple/ansible/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /pypi/simple/ansible/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /pypi/simple/ansible/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /pypi/simple/ansible/
  Could not fetch URL https://mirrors.163.com/pypi/simple/ansible/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='mirrors.163.com', port=443): Max retries exceeded with url: /pypi/simple/ansible/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)) - skipping
  Could not find a version that satisfies the requirement ansible (from versions: )
No matching distribution found for ansible
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://mirrors.163.com/pypi/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='mirrors.163.com', port=443): Max retries exceeded with url: /pypi/simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)) - skipping
[root@Python36 ~]#

原因
這里是ssl模塊的問題,系統(tǒng)上現(xiàn)在有兩個版本的python。使用我們編譯安裝的python3無法導(dǎo)入ssl模塊:

[root@Python36 ~]# python3
Python 3.6.6 (default, Sep 29 2018, 16:19:00) 
[GCC ④.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/python3/lib/python3.6/ssl.py", line 101, in <module>
    import _ssl             # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'
>>> exit()  
[root@Python36 ~]# python
Python 2.7.5 (default, Apr 11 2018, 07:36:10) 
[GCC ④.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> exit()
[root@Python36 ~]# 

在./configure過程中,如果沒有加上–with-ssl參數(shù)時,默認安裝的軟件涉及到ssl的功能不可用,剛好pip3過程需要ssl模塊,而由于沒有指定,所以該功能不可用。

解決辦法
就是多加一個參數(shù)--with-ssl,重新再做一遍編譯安裝。我這里也是全部安裝完成之后才發(fā)現(xiàn)有問題的,不過沒有關(guān)系,把之前編譯安裝的步驟再重新做一遍:

yum install openssl-devel
cd Python-3.6.6
./configure prefix=/usr/local/python3 --with-ssl
sed -e "s,@EXENAME@,/usr/local/python3/bin/python3.6m," < ./Misc/python-config.in >python-config.py
make
make install

在第一次編譯安裝3.6.6的時候沒有印象安裝個 openssl-devel 這個包,有可能是之前已經(jīng)安裝過了。不過最近在一臺新的系統(tǒng)上編譯安裝3.6.8的時候發(fā)現(xiàn)一定要在編譯之前把 openssl-devel 這個包安裝好才行。

然后驗證一下:

[root@Python36 ~]# python3
Python 3.6.6 (default, Aug 21 2019, 11:43:46) 
[GCC ④.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> exit()
[root@Python36 ~]# 

編譯python3.7遇到的問題

編譯python3.7.4的時候,在make的過程中又出現(xiàn)了新的報錯:

Failed to build these modules:
_ctypes 

因為python3.7版本需要一個新的包libffi-devel,再yum安裝一下即可:

$ yum install libffi-devel 

裝完之后,再運行一下make。

yum安裝

用yum的話,步驟簡單多了,就是安裝路徑會分散在不同的目錄里。

安裝epel源

用下面的命令就可以安裝好epel源。不過國外的源可能會比較慢,可以都換成國內(nèi)的yum源。

[root@Python3 ~]# yum install epel-release

上面的步驟就不要做了。
先確認有沒有wget命令:

[root@Python3 ~]# wget --version

如果沒有就先裝一下,之后下載yum源的文件要用:

[root@Python3 ~]# yum install wget

備份現(xiàn)在的yum源
備份是個好習(xí)慣,直接刪也可以。然后發(fā)現(xiàn)wget沒裝,現(xiàn)在一個yum源也沒有了,只能找其他辦法了。

[root@Python3 ~]# cd /etc/yum.repos.d/
[root@Python3 yum.repos.d]# mkdir bak
[root@Python3 yum.repos.d]# mv *.repo bak
[root@Python3 yum.repos.d]# ls
bak

現(xiàn)在文件夾已經(jīng)空了,去下載yum源的repo文件,我用的是阿里的源:

[root@Python3 yum.repos.d]# wget https://mirrors.aliyun.com/repo/Centos-7.repo
[root@Python3 yum.repos.d]# wget https://mirrors.aliyun.com/repo/epel-7.repo

然后可以makecache建立本地緩存:

[root@Python3 yum.repos.d]# yum makecache

安裝python

這里裝了python,另外還裝了一個setuptools,之后的pip工具要通過setuptools來安裝。

[root@Python3 yum.repos.d]# yum install python36 python36-setuptools

安裝完之后要用python36命令來啟動,這里可以添加一下python3命令的軟連接:

[root@Python3 ~]# ln -s python36 /usr/bin/python3

安裝pip

通過setuptools的easyinstall命令來安裝pip:

[root@Python3 ~]# easy_install-3.6 pip

安裝目錄

python 和 setuptools 都在這個目錄里:
/usr/lib/python3.6/site-packages/
而 pip 是在下面這個目錄里,就是默認編譯安裝的目錄:
/usr/local/lib/python3.6/site-packages
之后用pip安裝的模塊也都會在pip的這個目錄里。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI