环境:CentOS release 6.8 (Final)
# 直接编译python3.7在使用pip3安装依赖的时候报错:
Can't connect to HTTPS URL because the SSL module is not available.
解决方法:
1.编译安装OpenSSL 1.0.2j版本并重新配置环境变量
下载OpenSSL源码包:
wget http://www.openssl.org/source/openssl-1.0.2j.tar.gz
解压缩,编译安装:
tar -zxvf openssl-1.0.2j.tar.gz
cd openssl-1.0.2j
./config --prefix=/usr/local/lab/openssl-1.0.2j shared zlib
make && make install
2.编译安装Python3,使用自定义的OpenSSL
下载Python3.7.3源码包:
wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
解压缩,编译安装:
tar -zxvf Python-3.7.3.tgz
cd Python-3.7.3
./configure --prefix=/usr/local/python373
在这一步之后,先不要着急运行make命令。先修改源码目录 Python-3.7.3/Modules/Setup 文件:
# Socket module helper for socket(2)
#_socket socketmodule.c
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/lab/openssl-1.0.2j/ #取消这一行的注释,并将原来的/usr/local/ssl改为我们新安装的openssl目录:/usr/local/lab/openssl-1.0.2j/
_ssl _ssl.c \ #取消这一行的注释
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ #取消这一行的注释
-L$(SSL)/lib -lssl -lcrypto #取消这一行的注释
# The crypt module is now disabled by default because it breaks builds
# on many systems (where -lcrypt is needed), e.g. Linux (I believe).
#_crypt _cryptmodule.c # -lcrypt # crypt(3); needs -lcrypt on some systems
修改完成以后,还需要创建两个指向动态链接库的软链接文件:
ln -s /usr/local/lab/openssl-1.0.2j/lib/libssl.so.1.0.0 /usr/lib64/libssl.so.1.0.0
ln -s /usr/local/lab/openssl-1.0.2j/lib/libcrypto.so.1.0.0 /usr/lib64/libcrypto.so.1.0.0
最后编译并安装:
make && make install
# 创建软连接
ln -s /usr/local/python373/bin/pip3 /usr/local/bin/pip3
ln -s /usr/local/python373/bin/python3 /usr/local/bin/python3
# 安装软件继续报错:
[root@srv3:/usr/local]# pip3 install requests
Collecting requests
/usr/local/python373/bin/python3.7: symbol lookup error: /usr/local/python373/bin/python3.7: undefined symbol: SSL_CTX_get0_param
# 找了各自资料发现都无法解决这个问题,问题很可能就出在openssl上
undefined symbol: SSL_CTX_get0_param 这个函数就是zlib源码中的函数,于是重新编译openssl
1.清理之前安装的openssl
rm -rf /usr/local/src/openssl-1.0.2j # 清理源码
rm -rf /usr/local/lab/openssl-1.0.2j # 清理安装程序
tar -zxvf openssl-1.0.2j.tar.gz
cd openssl-1.0.2j
# 修改编译参数,no-zlib 不需要zlib
./config --prefix=/usr/local/lab/openssl-1.0.2j no-zlib
make && make install
2.重新编译安装Python3.7.3
# 清理源码目录和已经按照的目录
rm -rf /usr/local/src/Python-3.7.3
rm -rf /usr/local/python373
# 再次解压编译安装
tar -zxvf Python-3.7.3.tgz
cd Python-3.7.3
./configure --prefix=/usr/local/python373
在这一步之后,先不要着急运行make命令,先修改源码目录 Python-3.7.3/Modules/Setup 文件:
# Socket module helper for socket(2)
#_socket socketmodule.c
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/lab/openssl-1.0.2j/ #取消这一行的注释,并将原来的/usr/local/ssl改为我们新安装的openssl目录:/usr/local/lab/openssl-1.0.2j/
_ssl _ssl.c \ #取消这一行的注释
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ #取消这一行的注释
-L$(SSL)/lib -lssl -lcrypto #取消这一行的注释
# The crypt module is now disabled by default because it breaks builds
# on many systems (where -lcrypt is needed), e.g. Linux (I believe).
#_crypt _cryptmodule.c # -lcrypt # crypt(3); needs -lcrypt on some systems
最后编译并安装:
make && make install
# 再次pip3安装依赖就不会报错了
[root@srv3:/usr/local/src/Python-3.7.3]# pip3 install requests
Collecting requests
Downloading https://files.pythonhosted.org/packages/51/bd/23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb/requests-2.22.0-py2.py3-none-any.whl (57kB)
100% |████████████████████████████████| 61kB 14.1MB/s
Collecting certifi>=2017.4.17 (from requests)
Downloading https://files.pythonhosted.org/packages/69/1b/b853c7a9d4f6a6d00749e94eb6f3a041e342a885b87340b79c1ef73e3a78/certifi-2019.6.16-py2.py3-none-any.whl (157kB)
100% |████████████████████████████████| 163kB 23.3MB/s
Collecting idna<2.9,>=2.5 (from requests)
Downloading https://files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl (58kB)
100% |████████████████████████████████| 61kB 19.8MB/s
Collecting chardet<3.1.0,>=3.0.2 (from requests)
Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
100% |████████████████████████████████| 143kB 23.9MB/s
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 (from requests)
Downloading https://files.pythonhosted.org/packages/e6/60/247f23a7121ae632d62811ba7f273d0e58972d75e58a94d329d51550a47d/urllib3-1.25.3-py2.py3-none-any.whl (150kB)
100% |████████████████████████████████| 153kB 24.8MB/s
Installing collected packages: certifi, idna, chardet, urllib3, requests
Successfully installed certifi-2019.6.16 chardet-3.0.4 idna-2.8 requests-2.22.0 urllib3-1.25.3