如何解决Python找不到ssl模块问题 No module named _ssl

当时在linux服务器上搭建python时,配置虚拟环境,创建mkvirtualenv_django时出现No module named _ssl,一整天都没有解决,最后安装配置了openssl才解决掉,给自己做个笔记,也让朋友们少踩个坑!希望可以帮到你们!
[root@hadoop01 ~]# mkvirtualenv django_env
Traceback (most recent call last):
  File "/usr/local/python3.10/bin/virtualenv", line 5, in <module>
    from virtualenv.__main__ import run_with_catch
  File "/usr/local/python3.10/lib/python3.10/site-packages/virtualenv/__init__.py", line 1, in <module>
    from .run import cli_run, session_via_cli
import ssl
  File "/usr/local/python3.10/lib/python3.10/ssl.py", line 98, in <module>
    import _ssl             # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'
解决方法:

1.下载openssl

[root@chen2 opt]#wget https://www.openssl.org/source/openssl-1.1.1n.tar.gz --no-check-certificate

2.解压并安装

[root@chen2 opt]#tar -zxvf openssl-1.1.1n.tar.gz
[root@chen2 openssl-1.1.1n]#cd openssl-1.1.1n
[root@chen2 openssl-1.1.1n]#./config --prefix=/usr/local/openssl
[root@chen2 openssl-1.1.1n]#make -j 2
[root@chen2 openssl-1.1.1n]#make install

3.创建openssl的软连接(很重要)

[root@chen2 openssl]#ln -sf /usr/local/openssl/bin/openssl /usr/bin/openssl

4.修改vi /etc/ld.so.conf下的配置文件

[root@chen2 openssl]#vi /etc/ld.so.conf
如下:
------------------------------------------
include ld.so.conf.d/*.conf              
/usr/local/openssl/lib                    
-------------------------------------------

5.测试openssl是否生效

[root@chen ~]# openssl version
OpenSSL 1.1.1n  15 Mar 2022
[root@chen ~]# 

6.这样就算完成了,再次创建环境变量时,就不会报错了!

接下来是我给自己看的捏,就把python搭建到虚拟机到创建环境变量的步骤都写下来,当做一个笔记以后看ba!感兴趣的朋友可以看一看哦!

1.解压python并安装

[root@chen opt]#wget https://repo.huaweicloud.***/python/3.10.4/Python-3.10.4.tgz
[root@chen opt]#tar xvzf Python-3.10.4.tgz
[root@chen Python-3.10.4]#./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl --with-openssl-rpath=auto
[root@chen Python-3.10.4]#make -j && make install

2.配置环境变量

[root@chen ~]#vi ~/.bash_profile
#修改下面这行
PATH=/usr/local/python3/bin:$PATH:$HOME/bin

3.创建pip3 和 python3 的软连接

[root@chen ~]#ln -sf /usr/local/python3/bin/pip3 /usr/bin/pip3
[root@chen ~]#ln -sf /usr/local/python3/bin/python3 /usr/bin/python3

4.测试是否更改成功(第二行显示python配置成功,第五行和第九行显示软连接成功!)

[root@chen2 ~]# python3 -V
Python 3.10.2
[root@chen2 ~]# ll 
[root@chen2 ~]# ll /usr/bin/ |grep python
lrwxrwxrwx.   1 root root         27 3月   5 22:12 pip3 -> /usr/local/python3/bin/pip3
lrwxrwxrwx.   1 root root          7 3月   6 2023 python -> python2
lrwxrwxrwx.   1 root root          9 3月   6 2023 python2 -> python2.7
-rwxr-xr-x.   1 root root       7144 10月 14 2020 python2.7
lrwxrwxrwx.   1 root root         31 3月   5 23:28 python3 -> /usr/local/python3/bin/python3/
-rwxr-xr-x.   2 root root      11328 11月 17 2020 python3.6
-rwxr-xr-x.   2 root root      11328 11月 17 2020 python3.6m
[root@chen2 ~]# 

5.配置pypi下载源(提高下载速度)

[root@chen2 ~]#mkdir ~/.pip
[root@chen2 ~]#touch ~/.pip/pip.conf
[root@chen2 ~]#vi ~/.pip/pip.conf
添加如下代码:
--------------------
[global]
index-url=https://pypi.tuna.tsinghua.edu.***/simple/
[install]
trusted-host=pypi.tuna.tsinghua.edu.***
--------------------

6.下载virtualenvwrapper

[root@chen2 ~]#pip3 install virtualenvwrapper

7.再次进入~/.bash_profile配置文件(指定的是自己的python安装路径!!!)

[root@chen2 ~]#vi ~/.bash_profile
添加如下代码:
-------------------------
export PATH
export VIRTUALENVWRAPPER_PYTHON=/usr/local/python3/bin/python3  
source /usr/local/python3/bin/virtualenvwrapper.sh
------------------------
[root@chen2 ~]#. ~/.bash_profile

8.创建环境变量

[root@chen2 ~]# mkvirtualenv demo
created virtual environment CPython3.10.2.final.0-64 in 694ms
  creator CPython3Posix(dest=/root/.virtualenvs/demo, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==23.0.1, setuptools==67.4.0, wheel==0.38.4
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
virtualenvwrapper.user_scripts creating /root/.virtualenvs/demo/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/demo/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/demo/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/demo/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/demo/bin/get_env_details
(demo) [root@chen2 ~]# 

9.好的捏,成功啦~~~开心 😇😇

转载请说明出处内容投诉
CSS教程_站长资源网 » 如何解决Python找不到ssl模块问题 No module named _ssl

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买