前言
自己在Linux安裝過很多次Python,每次都會由于各種各樣的原因出現很多問題,很無奈
安裝流程
- 去這里先看看有哪些可用版本可用下載
- 下載
sudo wget http://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
- 解壓
sudo tar -xzvf Python-3.7.0.tgz
- 自定義安裝目錄
sudo mkdir /usr/local/python3
- 編譯、安裝
cd Python-3.7.0
sudo ./configure --prefix=/usr/local/python3
sudo make
sudo make install
- 預安裝所需要的庫
sudo yum -y install zlib zlib-devel
sudo yum -y install bzip2 bzip2-devel
sudo yum -y install ncurses ncurses-devel
sudo yum -y install readline readline-devel
sudo yum -y install openssl openssl-devel
sudo yum -y install openssl-static
sudo yum -y install xz lzma xz-devel
sudo yum -y install sqlite sqlite-devel
sudo yum -y install gdbm gdbm-devel
sudo yum -y install tk tk-devel
這些包安裝好,后面出現的問題就比較少了
源碼安裝程序主要出現問題的就是make install這一步
出現的問題(如果上面預安裝的軟件都安裝了,下面好多問題就沒有了)
-
Python build finished, but the necessary bits to build these modules were _bz2 _curses _curses_panel _dbm _gdbm _hashlib _lzma _sqlite3 _ssl _tkinter _uuid readline zlib
解決方法:
sudo yum install _bz2 _curses _curses_panel _dbm _gdbm _hashlib _lzma _sqlite3 _ssl _tkinter _uuid readline zlib
然后再sudo make install,一如既往的報錯
-
zipimport.ZipImportError: can’t decompress data; zlib not available
我在上面明顯安裝了,但是還報沒找到,那就用老套路
sudo yum install zlib* -y
此處有可能會報
Error: Multilib version problems found. This often means that the root
cause is something else and multilib version checking is just
pointing out that there is a problem.
解決方法:
sudo yum install -y zlib* --setopt=protected_multilib=false
然后再sudo make install,一如既往的報錯
-
ModuleNotFoundError: No module named ‘_ctypes’
網上大部分人都是通過如下解決的:
sudo yum install libffi-devel -y
但是我就不行,可能是yum源的問題,于是我又使用了慣用的招數
sudo yum install libffi* -y
總算成功了
注意:每次yum完,無論失敗與否都要執行make install
編譯、安裝完成后,建立軟連接
sudo ln -s /usr/local/python3/bin/python3.7 /usr/bin/python3
sudo ln -s /usr/local/python3/bin/pip3.7 /usr/bin/pip3
測試Python3.7
[analysis@master01 ~]$ python3
Python 3.7.0 (default, May 14 2019, 16:35:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
[analysis@master01 ~]$ pip3 -V
pip 10.0.1 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)
[analysis@master01 ~]$
卸載
先找到所有的python3相關的文件
whereis python3*
然后刪除
#移除Python3
rm -rf /usr/local/python37
#移除Python3軟鏈接
rm -rf /usr/bin/python3
Require TLS/SSL
本來以為很開心的就可以玩耍了,誰知道,使用pip3 install pandas竟然報錯:
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting virtualenv
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.")': /simple/virtualenv/
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.")': /simple/virtualenv/
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.")': /simple/virtualenv/
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.")': /simple/virtualenv/
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.")': /simple/virtualenv/
Could not fetch URL https://pypi.org/simple/virtualenv/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/virtualenv/ (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 virtualenv (from versions: )
No matching distribution found for virtualenv
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
原因
系統版本centos6.5,其中openssl的版本為OpenSSL 1.0.1e-fips 11 Feb 2013,而python3.7需要的openssl的版本為1.0.2或者1.1.x,需要對openssl進行升級,并重新編譯python3.7.0。yum 安裝的openssl 版本都比較低。
升級openssl
# 1.下賊openssl
sudo wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz
sudo tar -zxvf openssl-1.1.1a.tar.gz
cd openssl-1.1.1a
# 2.編譯安裝
sudo ./config --prefix=/usr/local/openssl no-zlib #不需要zlib
sudo make
sudo make install
# 3.備份原配置(如果報錯:沒有這個/usr/include/openssl文件,那么先執行第4部,然后再執行第3步)
sudo mv /usr/bin/openssl /usr/bin/openssl.bak
sudo mv /usr/include/openssl /usr/include/openssl.bak
# 4.新版配置
sudo ln -s /usr/local/openssl/include/openssl /usr/include/openssl
sudo ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/local/lib64/libssl.so
sudo ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
# 5.修改系統配置
## 寫入openssl庫文件的搜索路徑(注意,需要修改/etc/ld.so.conf文件的權限,不然無法追加進去)
sudo echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
## 使修改后的/etc/ld.so.conf生效
sudo ldconfig -v
# 6.查看openssl版本
openssl version
重新安裝python
sudo ./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl
sudo make
sudo make install
安裝需要的庫
sudo pip3 install pandas
sudo pip3 install numpy
sudo pip3 install matplotlib
sudo pip3 install scikit-learn
后記
由于環境配置不同,每個人遇到的問題都不同,只有針對下藥才能解決問題
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
