Windows anaconda python3 import ssl报错的解决方案

Windows anaconda python3 import ssl报错的解决方案

问题重现

引用ssl报错ModuleNotFoundError: No module named ‘_ssl’

>>> import ssl
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Anaconda\lib\ssl.py", line 98, in <module>
    import _ssl             # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'
>>>

同时对pip安装东西也造成了影响

C:\Users\YUZI>pip install jieba
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting jieba
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/jieba/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/jieba/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/jieba/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/jieba/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/jieba/
  Could not fetch URL https://pypi.org/simple/jieba/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/jieba/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
  Could not find a version that satisfies the requirement jieba (from versions: )
No matching distribution found for jieba

解决方案

我也搜了不少东西,但大多数都是在Linux上的解决办法。
在Windows上其实更简单。只要替换 _ssl.pyd 这个文件就行
anaconda的目录为 D:\Anaconda\DLLs\_ssl.pyd
Python的官网中下载相对应的版本的Python ZIP文件。
例如:
查询本机使用的Python版本
Windows anaconda python3 import ssl报错的解决方案去官网找到相对应的下载页面,这里使用的是Python3.7.2并下载ZIP包
Windows anaconda python3 import ssl报错的解决方案下载完成后解压,并找到我们需要的文件
Windows anaconda python3 import ssl报错的解决方案
复制并替换到Anaconda中,我的目录为 D:\Anaconda\DLLs\_ssl.pyd并测试import与pip

Windows anaconda python3 import ssl报错的解决方案

成功!!