您好,登錄后才能下訂單哦!
先看編譯安裝,編譯安裝后的文件目錄基本和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... 已連接。
已發出 HTTP 請求,正在等待回應... 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
如果缺少依賴包,編輯的時候就會有問題。到時候就查看錯誤信息,到網上搜一下,看看是缺少了哪個依賴包:
[root@Python36 download]# cd Python-3.6.6
[root@Python36 Python-3.6.6]# ./configure prefix=/usr/local/python3
注意,這里的編譯命令有點問題,還需要加一個參數--with-ssl
。否則python可以順利安裝并且正常使用,但是運行pip的時候會有問題。具體在這節編譯安裝的最后加了一個小節pip3報錯進行說明。
這里可以就按上面的命令來,然后參考最后一小節的方法來解決。不過既然已經知道有問題了,就把參數加上。把上面的命令替換為這個,多加一個參數:
yum install openssl-devel
./configure prefix=/usr/local/python3 --with-ssl
在編譯安裝python3.6.8的時候,需要在編譯之前先把 openssl-devel 這個包也安裝好。否則加了參數也是沒用的
指定安裝目錄是:/usr/local/python3。
如果不指定安裝目錄的話,安裝后可執行文件默認放在/usr/local/bin(默認放這里的可執行文件后面就不用另外再創建軟鏈接了),庫文件默認放在/usr/local/lib,配置文件默認放在/usr/local/etc,其它的資源文件放在/usr/local/share,比較凌亂。似乎也不是太亂的樣子,不過網上的文章都推薦在默認位置下再建一級目錄,全部放一起,就是這里的做法。
我們指定安裝目錄,以后卸載的話直接刪除目錄就可以干凈卸載了。
然后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]#
直接根據提示運行上面給的命令,然后再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()
創建軟鏈接的方法不是很好,還可以通過修改環境變量PATH來解決。不過還是先看下如何通過創建軟鏈接解決問題。
直接執行python3命令是找不到的,在python3的啟動命令加一條軟鏈接到系統目錄:
[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)
上面軟鏈接,一次只解決了一個命令的問題。其實/usr/local/python3/bin/目錄下所有的文件都是可執行文件,都需要設置為可以執行。當然也可以批量創建軟鏈接,把 /usr/local/python3/bin/目錄下的所有文件都創建一個軟鏈接。但是這么做一方面很煩,而且解決不了將來的問題。因為將來使用pip安裝時生成的可執行文件都會放到 /usr/local/python3/bin/目錄中,到時候還是需要再創建軟鏈接。
產生問題的原因
會出現這個問題是因為安裝的時候我們手動指定了安裝目錄,原本這些文件都是在/usr/local/bin/目錄下的,而這個目錄里的文件都是可以直接執行的。
系統的環境變量PATH,指定了哪些目錄用來存放可執行文件,這些目錄都的文件都是在全局可以直接調用執行的。查看變量值的方法如下:
[root@Python36 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@Python36 ~]#
設置環境變量
這里在profile中設置PATH,修改全局的環境變量。
編輯/etc/profile文件,找到export行,在下面新增加一行:
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCOntrOL
export PATH=$PATH:/usr/local/python3/bin
第一行是文件中原本的內容,第二行是我們增加的內容。
修改不會立刻生效,如果需要立即生效的話,可以執行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命令啟動python2給替換成啟動python3。這樣做的問題是,系統的yum使用要python2,因此替換為python3后,yum就無法正常工作了。
解決辦法就是修改yum的配置文件 vi /usr/bin/yum
,然后找到開頭的 #!/usr/bin/python
修改為 #!/usr/bin/python2.7
保存退出就好了。
按照這里上面介紹的方法安裝完成后,在使用pip的時候會出現報錯:
[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模塊的問題,系統上現在有兩個版本的python。使用我們編譯安裝的python3無法導入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參數時,默認安裝的軟件涉及到ssl的功能不可用,剛好pip3過程需要ssl模塊,而由于沒有指定,所以該功能不可用。
解決辦法
就是多加一個參數--with-ssl
,重新再做一遍編譯安裝。我這里也是全部安裝完成之后才發現有問題的,不過沒有關系,把之前編譯安裝的步驟再重新做一遍:
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 這個包,有可能是之前已經安裝過了。不過最近在一臺新的系統上編譯安裝3.6.8的時候發現一定要在編譯之前把 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.4的時候,在make的過程中又出現了新的報錯:
Failed to build these modules:
_ctypes
因為python3.7版本需要一個新的包libffi-devel,再yum安裝一下即可:
$ yum install libffi-devel
裝完之后,再運行一下make。
用yum的話,步驟簡單多了,就是安裝路徑會分散在不同的目錄里。
用下面的命令就可以安裝好epel源。不過國外的源可能會比較慢,可以都換成國內的yum源。
[root@Python3 ~]# yum install epel-release
上面的步驟就不要做了。
先確認有沒有wget命令:
[root@Python3 ~]# wget --version
如果沒有就先裝一下,之后下載yum源的文件要用:
[root@Python3 ~]# yum install wget
備份現在的yum源
備份是個好習慣,直接刪也可以。然后發現wget沒裝,現在一個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
現在文件夾已經空了,去下載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,另外還裝了一個setuptools,之后的pip工具要通過setuptools來安裝。
[root@Python3 yum.repos.d]# yum install python36 python36-setuptools
安裝完之后要用python36命令來啟動,這里可以添加一下python3命令的軟連接:
[root@Python3 ~]# ln -s python36 /usr/bin/python3
通過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的這個目錄里。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。