centos7 python3.12.1 报错 No module named _ssl,坑我几个小时

网上的教程很多,但几乎是无效的,大部分是教大家改Modole/Setup文件,大家千万不要这么做,正确的办法如下:有用的话就收藏吧 😄

常见的错误如下:
 File "/usr/lib/python3.4/urllib/request.py", line 1244, in unknown_open
    raise URLError('unknown url type: %s' % type)
**urllib.error.URLError: <urlopen error unknown url type: 'https>**


>>> import ssl
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/python3/lib/python3.6/ssl.py", line 101, in <module>
    import _ssl             # if we can't import it, let the error propagate
**ModuleNotFoundError: No module named '_ssl'**
  • 安装依赖包
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel
  • 安装wget
yum install wget
  • 安装或更新openssl - https://github.com/openssl/openssl/tags

注意不要安装太高版本的 openssl,否则会坑你半天
编译时报错日志如下:
checking for include/openssl/ssl.h in /us/local/openssl… yes
checking whether compiling and linking against OpenSSL works… yes
checking for --with-openssl-rpath… auto
provides whether OpenSSL provides required ssl module APIs no
provides whether OpenSSL provides required hashlib module APIs no
checking for --with-ssl-default-suites… python
checking for --with-builtin-hashlib-hashes… md5, shal, sha2, sha3, blake2
以下教程在openssl:1.1.1w版本及python3.12.1 测试成功

cd /usr/local/src
wget https://github.com/openssl/openssl/releases/download/OpenSSL_1_1_1w/openssl-1.1.1w.tar.gz
tar -zxvf openssl-1.1.1w.tar.gz
cd openssl-1.1.1w/
./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl
make && make test && make install

mv /usr/bin/openssl /usr/bin/openssl_backup
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib/libssl.so.1.1
ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib/libcrypto.so.1.1
openssl version
  • 安装python3 - https://www.python.org/downloads/
cd /usr/local/src
wget https://www.python.org/ftp/python/3.12.1/Python-3.12.1.tgz
tar -zxvf Python-3.12.1.tgz
cd Python-3.12.1/
./configure -C --with-openssl=/usr/local/openssl --with-openssl-rpath=auto --prefix=/usr/local/python3
# 调试用: ./configure -C --with-openssl=/usr/local/openssl --with-openssl-rpath=auto --prefix=/usr/local/python3 2>&1 | tee configure.log
make -j8
# 调试用: make 2>&1 | tee make.log
make altinstall
# 调试用: make altinstall 2>&1 | tee install.log
ln -s /usr/local/python3/bin/python3.12 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.12 /usr/bin/pip3
ln -s /usr/local/python3/bin/python3.12-config /usr/bin/python3-config
python3 -V
  • 验证ssl_ssl是否安装成功
[root@tj1-miui-apm-vm028 bin]# python3
Python 3.12.1 (main, Jan 16 2024, 15:02:11) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> import _ssl
  • 17
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值