Saki's 研究记录

No module named '_ssl'

字数统计: 434阅读时长: 2 min
2021/11/27

环境

系统: CentOS Linux release 8.4.2105 (Core)

背景

安装后 Jupyter Notebook后,执行jupyter notebook出现如下报错:

1
2
3
4
5
6
7
8
9
10
11
jupyter notebook
Traceback (most recent call last):
File "/usr/local/python3/bin/jupyter-notebook", line 5, in <module>
from notebook.notebookapp import main
File "/usr/local/python3/lib/python3.9/site-packages/notebook/notebookapp.py", line 60, in <module>
from tornado import httpserver
File "/usr/local/python3/lib/python3.9/site-packages/tornado/httpserver.py", line 29, in <module>
import ssl
File "/usr/local/python3/lib/python3.9/ssl.py", line 98, in <module>
import _ssl # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'

执行以下命令安装ssl相关组件后,依旧报错。

1
yum install -y openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc-c++ gcc openssl-devel

根据google大法,了解到需要添加ssl支持后重新安装python可以解决,亲测有效。

下载Python3.9.5

1
2
wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tgz
tar zxvf Python-3.9.5.tgz

安装必须的包

1
dnf -y install gcc libffi-devel

添加ssl支持

编辑Python-3.9.5/Modules/Setup文件,修改或直接添加以下信息。

1
2
3
4
5
_socket socketmodule.c
SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto

安装

1
2
3
4
cd Python-3.9.5/
mkdir /usr/local/python3/
./configure --prefix=/usr/local/python3 --enable-optimizations
make && make install

替换原先的 Python3 和 pip3

1
2
3
4
mv /usr/bin/python3 /usr/bin/python.bak
mv /usr/bin/pip3 /usr/bin/pip.bak
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

添加环境变量

1
2
echo "export PATH=/usr/local/python3/bin:$PATH">>/root/.bashrc
source /root/.bashrc

执行python3进行测试

1
2
3
4
5
6
python3
Python 3.9.5 (default, Oct 15 2021, 16:44:21)
[GCC 8.4.1 20200928 (Red Hat 8.4.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import _ssl
>>>

不再报错了,问题解决~

Done.

CATALOG
  1. 1. 环境
  2. 2. 背景
  3. 3. 下载Python3.9.5
  4. 4. 安装必须的包
  5. 5. 添加ssl支持
  6. 6. 安装
  7. 7. 替换原先的 Python3 和 pip3
  8. 8. 添加环境变量
  9. 9. 执行python3进行测试