关于在使用 pip 出现找不到 ssl 模块的问题。
在网上有很多人说在编译的时候需要添加 --with-ssl ,但是在添加之后确实也会出现警告configure: WARNING: unrecognized options: --with-ssl
又有人说这个选项已经不支持了,没有作用了。但是我在3.6.8版本时候的时候,虽然也是出现了警告,但是当我继续编译安装之后,引用ssl模块是可行的。
[root@root83 Python-3.6.8]# ./configure --prefix=/usr/local/Python3.6/ --enable-optimizations --enable-loadable-sqlite-extensions --with-ssl
...
configure: WARNING: unrecognized options: --with-ssl #会出现--with-ssl警告
...
[root@root83 Python-3.6.8]# make && make install
...
(cd /usr/local/Python3.6/bin; ln -s pyvenv-3.6 pyvenv)
if test "x" != "x" ; then \
rm -f /usr/local/Python3.6/bin/python3-32; \
(cd /usr/local/Python3.6/bin; ln -s python3.6-32 python3-32) \
fi
rm -f /usr/local/Python3.6/share/man/man1/python3.1
(cd /usr/local/Python3.6/share/man/man1; ln -s python3.6.1 python3.1)
if test "xupgrade" != "xno" ; then \
case upgrade in \
upgrade) ensurepip="--upgrade" ;; \
install|*) ensurepip="" ;; \
esac; \
./python -E -m ensurepip \
$ensurepip --root=/ ; \
fi
Looking in links: /tmp/tmpuzdf0qz1
Requirement already up-to-date: setuptools in /usr/local/Python3.6/lib/python3.6/site-packages (40.6.2)
Requirement already up-to-date: pip in /usr/local/Python3.6/lib/python3.6/site-packages (18.1)
# 以上是编译安装完成
(py3) [root@root83 Python-3.6.8]# python3.6
Python 3.6.8 (default, Jun 10 2019, 23:48:04)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>>
# 以上是成功引用模块
如果不想使用 --with-ssl
选项,可以参考https://chowyi.com/%E6%BA%90%E7%A0%81%E7%BC%96%E8%AF%91%E5%AE%89%E8%A3%85Python3%E5%8F%8A%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3/ 修改Python源码 setup.py 文件,更改引用ssl模块的路径。
CentOS 7.6 系统的模块一般都是安装在 /usr/include/ 目录下。
ython 源码 引用 ssl 模块的路径可以改为:
# Detect SSL support for the socket module (via _ssl)
search_for_ssl_incs_in = [
'/usr/local/ssl/include',
'/usr/include/openssl', # 添加此行,否则可能会报错找不到 rsa.h 文件
'/usr/contrib/ssl/include/'
修改Python3.6.1/Module/Setup.dist文件:
# line: 207
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
把上面的四行注释放开,然后再执行:
[root@root83 Python-3.6.8]# ./configure --prefix=/usr/local/Python3.6/ --enable-optimizations --enable-loadable-sqlite-extensions
[root@root83 Python-3.6.8]# make # 注意make之后的日志 ssl 模块是否加载成功。
[root@root83 Python-3.6.8]# make install
方法参考自:http://stackoverflow.com/questions/5937337/building-python-with-ssl-support-in-non-standard-location
http://stackoverflow.com/questions/5128845/importerror-no-module-named-ssl
注意:修改 Python 源码的方法可能会造成不必要的麻烦。