1. Python 3.8.1安裝
源碼安裝常規操作:
wget -c https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz
tar -xvf Python-3.8.1.tgz
mkdir -p /my/python/
cd Python-3.8.1
./configure --prefix="/my/python/"
make
make install
make后沒報錯,但出現了如下提示:
Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381
下載libssl(替代openssl)
https://ftp.openbsd.org/pub/OpenBSD/LibreSSL/
源碼編譯安裝成功。我想指定libssl來編譯python3,做了如下嘗試:
export LDFLAGS=" -L/zfssz3/SP_MSI/USER/pengjianxiang/software/INSTALL/LibreSSL23/lib"
export CPPFLAGS=" -I/zfssz3/SP_MSI/USER/pengjianxiang/software/INSTALL/LibreSSL23/include"
export PKG_CONFIG_PATH="/zfssz3/SP_MSI/USER/pengjianxiang/software/INSTALL/LibreSSL23/lib/pkgconfig"
然后加參數試試。
./configure –prefix=/my/python –enable-shared CFLAGS=-fPIC #提示沒這個參數
./configure –prefix=/my/python --with-openssl=/my/libssl/path #編譯報錯
看一些教程,修改Modules/Setup文件,把這些行取消注釋。
#SSL=/usr/local/ssl #改為我安裝的ssl路徑,嘗試了也沒用
#_ssl _ssl.c \
# -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
# -L$(SSL)/lib -lssl -lcrypto
以上方法都不行。
最后干脆忽略make提示的這一句,反正它又不是error,直接安裝。裝成功了。

Python 3.8.1 (default, Feb 12 2020, 13:03:12)
[GCC 4.9.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello python3")
hello python3
>>>
但是這個SSL的問題在后續會顯現。有答案(https://www.cnblogs.com/zhangweiyi/p/10614658.html)說安裝python時,應該加這么個參數,否則后續有很多包安裝不上:
./configure --prefix=/my/path/python38 --with-ssl # pip3會用到ssl模塊,沒有指定的話后續該功能不能使用
2. Python3配置及其包安裝管理
將python3加入環境變量
系統中我還有個python2.7在用,也加入了環境變量。為了使用的時候能區分版本,加入的時候要注意名字的差異。
python2的bin目錄如下,可知用python/python2以及pip/pip2均可:

成功安裝后的python3的bin目錄如下,可以用python3/python3.8以及pip3/pip3.8。

如果安裝的python3和python2有重名的話,最好是重命名一下區分后再加入環境中。這里因為我安裝后的名字本來就有區分,就可直接加入環境變量了。
環境變量在bash_profile或bashrc中設置都可以,我習慣了bashrc了:
vim ~/.bashrc
# python3/pip3 環境變量
export PATH=/my/path/Python38/bin:$PATH
source ~/.bashrc
查看下是否可以:
which python
>>>/my/path/Python-2.7.15/bin/python
which python2
>>>/my/path/Python-2.7.15/bin/python2
which pip
>>>/my/path/Python-2.7.15/bin/pip
which pip2
>>>/my/path/Python-2.7.15/bin/pip2
which python3
>>>/my/path/Python38/bin/python3
which pip3
>>>/my/path/Python38/bin/pip3
所以若要用python2時,使用python或python2;若用python3時,使用python3即可。
包的路徑設置
除了python要設置外,python包的位置也需要定義,不同版本無法相互安裝,容易發生沖突。
export PYTHONPATH=/my/path/Python38/lib/python3.8/site-packages
site-packages是python默認安裝包的位置,可查看已安裝哪些包。也可用pip3 list查看安裝包列表。

包的安裝
設置python3的包路徑之后,有些包如果直接用pip3 install安裝也會出現問題。
建議用某一個版本安裝包時,最好是在.bashrc將另一個版本的python和pythonpath注釋掉,以免沖突。安裝完成或者需要互換時再切換回來。
pip3 install numpy
ERROR: Could not find a version that satisfies the requirement numpy (from versions: none)
ERROR: No matching distribution found for numpy
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
這很有可能是網絡的問題,需要使用國內的鏡像源來加速,比如豆瓣源。
pip3 install numpy -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

總之,安裝包首先需要聯網,如果安裝失敗,多種方法都可以嘗試下,包括加國內鏡像源,或者換鏡像源:
pip3 install numpy
pip3 install numpy -i http://pypi.douban.com/simple
pip3 install numpy -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip3 install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
國內的鏡像源有:
清華:https://pypi.tuna.tsinghua.edu.cn/simple
阿里雲:http://mirrors.aliyun.com/pypi/simple/
中國科技大學 https://pypi.mirrors.ustc.edu.cn/simple/
華中理工大學:http://pypi.hustunique.com/
山東理工大學:http://pypi.sdutlinux.org/
豆瓣:http://pypi.douban.com/simple/
如果覺得每次安裝包都需要加鏡像源很麻煩,也可進行一次性永久性修改,在~/.pip/pip.conf (沒有就創建一個文件夾及文件。文件夾要加“.”,表示是隱藏文件夾)中添加內容:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com
但不建議這么做,因為我覺得各個鏡像源的包都不是很全,安裝時可以互相補充。很多包其實用默認的反而能安裝成功。
如果不確定包名(python2可能與python3有出入),可用pip search package來查看,再安裝功能相同的包。比如smtplib模塊(發送郵件),如果直接用install是安裝不上的:
$ pip3 install smtplib
ERROR: Could not find a version that satisfies the requirement smtplib (from versions: none)
ERROR: No matching distribution found for smtplib
pip3 search smtplib后,可以看到這么多功能類似的包。這里我們安裝PyEmail。

就可以直接導入smtplib模塊了。
Python 3.8.1 (default, Feb 13 2020, 10:49:45)
[GCC 4.9.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import OptionParser
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'OptionParser'
>>> import smtplib
>>> exit()
同樣地,如果直接安裝MIMEMultipart包會失敗,search一下。

安裝這個包后,可導入我們之前用的許多python2模塊了。
Python 3.8.1 (default, Feb 13 2020, 10:49:45)
[GCC 4.9.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import email.mime.multipart
>>> from optparse import OptionParser
>>> from email.mime.multipart import MIMEMultipart
>>> from email.mime.text import MIMEText
導入包
安裝好的包導入試試:

問題
很多包裝不上,報如下錯:
WARNING: 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/optionparser/
Could not fetch URL https://pypi.mirrors.ustc.edu.cn/simple/optionparser/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.mirrors.ustc.edu.cn', port=443): Max retries exceeded with url: /simple/optionparser/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
ERROR: Could not find a version that satisfies the requirement optionparser (from versions: none)
ERROR: No matching distribution found for optionparser
WARNING: You are using pip version 19.2.3, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
簡單說就是Can't connect to HTTPS URL because the SSL module is not available,這是因為安裝python時未能安裝ssl的遺留問題(上面那個答案說加--with-ssl選項,然而並沒有這個選項)。python3的ssl模塊(ssl用來采集https后綴的鏈接)必須用openssl並且版本必須大於等於1.02或者libressl2.64(不含)以后的版本,而默認linux的是1.01。有回答說openssl貌似有漏洞,建議用libressl來替代。

系統自帶的openssl版本與python3的版本不匹配,所以這里只要安裝libressl就可以解決問題。網上教程都是有root權限修改配置文件,再建立硬鏈接,但我因為沒有root權限,還是比較麻煩的。
下一篇(Linux非root安裝Python3以及解決SSL問題)將通過升級openssl,並重新編譯python3來徹底解決這個問題。
Ref: https://blog.csdn.net/love_cjiajia/article/details/82254371
https://www.cnblogs.com/Caiyundo/p/9469711.html
https://blog.csdn.net/qq_38486203/article/details/88864506
https://www.cnblogs.com/lijinze-tsinghua/p/8666558.html
https://www.cnblogs.com/songzhixue/p/11296720.html
https://www.cnblogs.com/mengzhilva/p/11059329.html
