Centos7安裝Python3.10

準(zhǔn)備環(huán)境

1.安裝依賴(lài)包

在編譯python的時(shí)候,需要一些依賴(lài)包,一次性安裝完

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel

2.安裝wget

yum install wget

安裝OpenSSL

這一步比較重要,在文檔中說(shuō)到需OpenSSL需要1.0.2或者1.1.x

Currently Python versions 3.6 to 3.9 are compatible with OpenSSL 1.0.2, 1.1.0, and 1.1.1.

1.下載OpenSSL

我們先下載最新版本并解壓,目前最新版本為1.1.1g

wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz
tar -zxvf openssl-1.1.1g.tar.gz

2.編譯安裝

分別執(zhí)行下面三條命令

cd openssl-1.1.1g
./config shared --openssldir=/usr/local/openssl \
   --prefix=/usr/local/openssl no-zlib  #不需要zlib
make -j4 && make install

3.備份原配置

mv /usr/bin/openssl /usr/bin/openssl.bak
mv /usr/include/openssl/ /usr/include/openssl.bak

4.配置

將安裝好的openssl 的openssl命令軟連到/usr/bin/openssl

ln -s /usr/local/openssl/include/openssl /usr/include/openssl
將軟鏈到升級(jí)后的libssl.so

ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/local/lib64/libssl.so
將安裝好的openssl命令軟連到/usr/bin/openssl

ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
將軟鏈接寫(xiě)入openssl庫(kù)文件的搜索路徑

echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
 
ldconfig -v #使修改后的/etc/ld.so.conf生效
/etc/ld.so.conf 這個(gè)文件記錄了編譯時(shí)使用的動(dòng)態(tài)鏈接庫(kù)的路徑,告訴鏈接器去哪個(gè)路徑下尋找鏈接時(shí)需要用到的庫(kù),如果找不到,就會(huì)提示鏈接錯(cuò)誤。

完成OpenSSL安裝
至此OpenSSL已經(jīng)安裝并配置好了,我們查看一下版本

openssl version

OpenSSL 1.1.1q 5 Jul 2022

安裝Python

1.下載源碼包

wget https://www.python.org/ftp/python/3.10.10/Python-3.10.10.tgz

這里下載的是3.10.10版本,如果需要裝其他版本可以到這里下載自己需要的版本。

注:如果這一步驟速度比較慢,可以先通過(guò)其他下載工具下載到本機(jī),然后通過(guò)scp上傳上去即可。

2.解壓

解壓壓縮包

tar -zxvf Python-3.10.10.tgz

進(jìn)入文件夾

cd Python-3.10.10

3.配置,編譯,安裝

./configure prefix=/usr/local/python3 -with-openssl=/usr/local/openssl #<---注意-with-openssl這個(gè)參數(shù)
make && make install

這個(gè)過(guò)程比較漫長(zhǎng),耐心等待即可

4.添加軟鏈接

添加python3軟鏈接

ln -s /usr/local/python3/bin/python3.10 /usr/bin/python3 

添加pip3的軟鏈接

ln -s /usr/local/python3/bin/pip3.10 /usr/bin/pip3

5.完成安裝

我們測(cè)試一下python3

python3 --version

6.安裝pipenv

pip3 install pipenv -i https://pypi.tuna.tsinghua.edu.cn/simple/
ln -s /usr/local/python3/bin/pipenv /usr/bin/pipenv
pipenv --version

Python 3.10.10常見(jiàn)問(wèn)題

如果您沒(méi)有安裝OpenSSL,直接安裝的Python,那么大概率您在使用pip3的時(shí)候會(huì)報(bào)錯(cuò),報(bào)錯(cuò)信息諸如pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.SSLError("Can't connect to HTTPSURL because the SSL module is not available.")之類(lèi)。

這種情況大概率就是缺少ssl模塊所致

驗(yàn)證很簡(jiǎn)單,大家可以進(jìn)入python3控制臺(tái),import ssl,這時(shí)候大概率會(huì)報(bào)異常。

大家可以嘗試使用安裝OpenSSL步驟安裝一遍,光這樣還是不夠的,安裝完之后,還要再進(jìn)行一次Python的編譯和安裝,這樣才能生效,還有就是配置后面的-with-openssl參數(shù)不能少。

這里我沒(méi)有鏈接到python上,是因?yàn)閥um要用到python2才能執(zhí)行,所以現(xiàn)在輸入python的話還是會(huì)進(jìn)入python2.7,輸入python3才會(huì)進(jìn)入python3.10

如果執(zhí)意想要鏈接到python的話,就得修改一下yum的配置:

vi /usr/bin/yum 
把 #! /usr/bin/python 修改為 #! /usr/bin/python2 
 
vi /usr/libexec/urlgrabber-ext-down 
把 #! /usr/bin/python 修改為 #! /usr/bin/python2
添加軟鏈接

添加軟鏈接
 
復(fù)制代碼
#將原來(lái)的鏈接備份
mv /usr/bin/python /usr/bin/python.bak
 
#添加python3的軟鏈接
ln -s /usr/local/python3/bin/python3.10 /usr/bin/python
 
#測(cè)試是否安裝成功了
python -V

其他

設(shè)置清華下載源

pip install scikit-learn  -i https://pypi.tuna.tsinghua.edu.cn/simple

pip install pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

下載安裝pip

curl -o  https://bootstrap.pypa.io/get-pip.py get-pip.py
python get-pip.py
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容