linux系統安裝Python 3.7.x


 1. 基礎環境。

[root@monitor ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 

 2. 升級openssl。CentOS 7.6默認自帶的openssl是1.0.2,需要升級到1.1.1,否則使用pip安裝模塊是會報錯“pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available”。(如果是先裝的Python,后升級的openssl,則升級完后需要重新編譯Python)

[root@monitor ~]# openssl version
OpenSSL 1.0.2k-fips  26 Jan 2017
# 安裝依賴。如果是CentOS 6.x的系統需要安裝xz,否則下面解壓Python壓縮包會報錯。
[root@monitor ~]# yum -y install gcc xz
# 下載新版本的openssl
[root@monitor ~]# cd /usr/local/src
[root@monitor src]# wget https://www.openssl.org/source/openssl-1.1.1c.tar.gz
[root@monitor src]# tar xf openssl-1.1.1c.tar.gz
[root@monitor src]# cd openssl-1.1.1c/
[root@monitor openssl-1.1.1c]# ./config --prefix=/usr/local/openssl
[root@monitor openssl-1.1.1c]# make
[root@monitor openssl-1.1.1c]# make install
# 備份原來的openssl,並為新版本的openssl創建軟鏈接。
[root@monitor openssl-1.1.1c]# mv /usr/bin/openssl{,.bak}
[root@monitor openssl-1.1.1c]# ln -s /usr/local/openssl/include/openssl /usr/include/openssl
[root@monitor openssl-1.1.1c]# ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/local/lib64/libssl.so
[root@monitor openssl-1.1.1c]# ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
# 寫入openssl庫文件的搜索路徑,並使之生效。
[root@monitor openssl-1.1.1c]# echo "/usr/local/openssl/lib" >>/etc/ld.so.conf
[root@monitor openssl-1.1.1c]# ldconfig -v
[root@monitor openssl-1.1.1c]# openssl version
OpenSSL 1.1.1c  28 May 2019

 3. 下載Python 3.7.x的tar包,並解壓。

[root@monitor openssl-1.1.1c]# cd /usr/local/src/
[root@monitor src]# wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz
[root@monitor src]# tar xf Python-3.7.2.tar.xz
[root@monitor src]# cd Python-3.7.2/

 4. 編譯安裝。

# 安裝依賴包。
[root@monitor Python-3.7.2]# yum -y install zlib-devel libffi-devel
[root@monitor Python-3.7.2]# ./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl
[root@monitor Python-3.7.2]# make && make install
Successfully installed pip-18.1 setuptools-40.6.2

 5. 配置環境變量。

[root@monitor python3]# cd /usr/local/python3/bin/
[root@monitor bin]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@monitor bin]# echo 'export PATH=/usr/local/python3/bin/:$PATH' >>/etc/profile
[root@monitor bin]# . /etc/profile
[root@monitor bin]# python3 -V
Python 3.7.2

 6. 測試pip安裝requests模塊。

[root@monitor bin]# pip3 install requests
Successfully installed certifi-2019.6.16 chardet-3.0.4 idna-2.8 requests-2.22.0 urllib3-1.25.3


寫作不易,轉載請注明出處,謝謝~~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM