前言:
这是一次在SentOs上使用Python3时,遇到的问题。因为项目开发,我需要在本地搭建的虚拟环境用pip中安装一些库,此时出现了pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
这个提示错误,按图索骥,找到是系统自带的openssl并不支持python3,因此,需要重新安装openssl
lisressl源码官方网站下载
提前安装依赖:
yum -y install zlib zlib-devel
yum -y install bzip2 bzip2-devel
yum -y install ncurses ncurses-devel
yum -y install readline readline-devel
yum -y install openssl openssl-devel
yum -y install openssl-static
yum -y install xz lzma xz-devel
yum -y install sqlite sqlite-devel
yum -y install gdbm gdbm-devel
yum -y install tk tk-devel
解压安装 libressl:
tar -zxvf libressl-3.1.0.tar.gz
cd libressl-3.1.0
./configure --prefix=/install_path # --prefix=/install_path 是指定安装位置,比如 /usr/local/ssl
make
make install
将ssl/lib的路径新增到库文件(这里的重要性在安装python的时候再说)
cd /etc/ld.so.conf.d
vim libressl-3.1.0.conf # 注意,这里的文件名是自己下载的libressl的版本
# 保存下面的内容 保存退出
/usr/local/src/mySoftware/ssl/lib # 这里的自定义libressl安装路径下的lib
# 重新加载库文件
ldconfig -v
创建软链接并检验成功与否
# 将旧版本的openssl的软链备份
mv /usr/bin/openssl /usr/bin/openssl.bak
mv /usr/include/openssl /usr/include/openssl.bak
# 为新安装的opnssl 创建软链。为什么将软链创建在 usr/local/bin/ ,是因为usr/local/bin/和/usr/bin/都在我的环境变量中,后面如果成功安装的话,原则上都可以运行openssl。但是我本地的/usr/bin/目录下创建新版openssl软链,不能成功运行. 这个问题等未来搞清了状况,再来此作记录。
ln -s /install_path/bin/openssl usr/local/bin/openssl
ln -s /install_path/include/openssl usr/local/include/openssl
openssl version #看能否打印成功版本信息
安装python
到这里问题就来了
cd Python-3.7.0
# 安装前对python安装目录进行配置,并检查安装环境
./configure --prefix=/usr/local/src/mySoftware/python3.7 --enable-shared --enable-optimizations CFLAGS=-fPIC
# 但始终显示下面这两条信息
checking whether compiling and linking against OpenSSL works... no
在这个网站上找到了答案
自己手动编辑的openssl, python3无法自己找到的话,需要设置 --with-openssl=[mydir]/openssl 参数,指定openssl的安装路径,
而此时不但没解决问题,还多出个问题,显示如下:
checking for openssl/ssl.h in [mydir]/openssl... no
checking whether compiling and linking against OpenSSL works... no
注意到--with-openssl=[mydir]/openssl
这个地方,这里到底是需要把openssl的完整路径添加进去,还是只需要到最上一层的安装目录就可以了呢?
测试:
./configure --prefix=/usr/local/src/mySoftware/python3.7 --with-openssl=/usr/local/src/mySoftware/ssl --enable-shared --enable-optimizations CFLAGS=-fPIC
# 检查最后的输出
checking for openssl/ssl.h in [mydir]/openssl... ok
checking whether compiling and linking against OpenSSL works... ok
配置测试成功!
再继续python3的安装
# 这里仍然是在python的解压目录里面
make
make install
cd bin
./python3 --version
# 这时,意想不到的错误又来了
./python3: error while loading shared libraries: libpython3.7m.so.1.0: cannot open shared object file: No such file or directory
通过检查其动态链接,查找原因
ldd /mydir/python3.7/bin/python3.7
在lib文件夹里面找到libpython3.7m.so.1.0
[root@master lib]# cp libpython3.7m.so.1.0 /usr/local/lib64/
[root@master lib]# cp libpython3.7m.so.1.0 /usr/lib/
[root@master lib]# cp libpython3.7m.so.1.0 /usr/lib64/
[root@master bin]# ./python3.7 --version
Python 3.7.0
接下来,进入python3的交互环境
python3
>>> import ssl # 检查ssl是否能正常导入
ModuleNotFoundError: No module named '_ssl'
这个地方的原因在于,我之前跳过了上面的 将ssl/lib的路径新增到库文件 这一步,所以做完这一步的工作后,再重新对python3进行编译
最后检查:
[root@slave1 src]# python3
Python 3.7.0 (default, Apr 22 2020, 08:57:55)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>>
整个过程耗时一天半,完美收官。
后记:在整个的python环境搭建中,这些问题的出现都是源于对Linux系统和python的内部执行机制不明确。一步步的排错过程也是一个学习的过程。虽然环境已经搭建成功,但仍然有很多的知识盲点。比如,对于为什么这么做还是懵懂得很。也是基于这一点,在后续的过程中,极有可能仍然会出现一些其他问题。
还有一点就是,遇到问题,通过各种博客论坛上的文章教程,十有八九会得以解决,但有可能不能根本性地解决每个人的困扰。因为同样的异常提示,不代表产生的原因相同。因此,尽量完整地记录整个排错过程,在后续的学习上不断补足知识盲区,再回来看以前出现的问题,相信会有更为深刻的见解和理解。
参考资料:
Python3.7安装(解决ssl问题)
网友评论