Python3.7运行ChatterBot遇OpenSSL版本兼容问题求助
问题:Python3.7运行ChatterBot示例代码报错
环境与依赖
- Python版本:3.7.0
- 开发工具:Visual Studio Code
- 已通过
pip install -r Chatterbot-masters/requirements.txt安装依赖:
mathparse>=0.1,<0.2 python-dateutil>=2.8,<2.9 sqlalchemy>=1.3,<1.4 pytz
示例代码
from chatterbot import ChatBot from chatterbot.trainers import ListTrainer # Create a new instance of a ChatBot bot = ChatBot( 'Example Bot', storage_adapter='chatterbot.storage.SQLStorageAdapter', logic_adapters=[ { 'import_path': 'chatterbot.logic.BestMatch', 'default_response': 'I am sorry, but I do not understand.', 'maximum_similarity_threshold': 0.90 } ] ) trainer = ListTrainer(bot) # Train the chat bot with a few responses trainer.train([ 'How can I help you?', 'I want to create a chat bot', 'Have you read the documentation?', 'No, I have not', 'This should help get you started: http://chatterbot.rtfd.org/en/latest/quickstart.html' ]) # Get a response for some unexpected input response = bot.get_response('How do I make an omelette?') print(response)
报错信息
运行python example.py时出现以下错误:
PS C:\Users\Joe\Desktop\Kindra> python example.py Traceback (most recent call last): File "example.py", line 13, in <module> 'maximum_similarity_threshold': 0.90 File "C:\Users\Joe\AppData\Local\Programs\Python\Python37\lib\site-packages\chatterbot\chatterbot.py", line 28, in __init__ self.storage = utils.initialize_class(storage_adapter, **kwargs) File "C:\Users\Joe\AppData\Local\Programs\Python\Python37\lib\site-packages\chatterbot\utils.py", line 33, in initialize_class return Class(*args, **kwargs) File "C:\Users\Joe\AppData\Local\Programs\Python\Python37\lib\site-packages\chatterbot\storage\sql_storage.py", line 20, in __init__ super().__init__(**kwargs) File "C:\Users\Joe\AppData\Local\Programs\Python\Python37\lib\site-packages\chatterbot\storage\storage_adapter.py", line 23, in __init__ 'tagger_language', languages.ENG File "C:\Users\Joe\AppData\Local\Programs\Python\Python37\lib\site-packages\chatterbot\tagging.py", line 20, in __init__ import spacy File "C:\Users\Joe\AppData\Local\Programs\Python\Python37\lib\site-packages\spacy\__init__.py", line 12, in <module> from .cli.info import info as cli_info File "C:\Users\Joe\AppData\Local\Programs\Python\Python37\lib\site-packages\spacy\cli\__init__.py", line 1, in <module> from .download import download # noqa: F401 File "C:\Users\Joe\AppData\Local\Programs\Python\Python37\lib\site-packages\spacy\cli\download.py", line 5, in <module> import requests File "C:\Users\Joe\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\__init__.py", line 43, in <module> import urllib3 File "C:\Users\Joe\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\__init__.py", line 39, in <module> "urllib3 v2.0 only supports OpenSSL 1.1.1+, currently " ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with OpenSSL 1.1.0h 27 Mar 2018. See: https://github.com/urllib3/urllib3/issues/2168
排查尝试
OpenSSL版本检查
- 执行
python -m OpenSSL.debug结果:
python -m OpenSSL.debug >> pyOpenSSL: 23.1.1 cryptography: 40.0.2 cffi: 1.15.1 cryptography's compiled against OpenSSL: OpenSSL 3.1.0 14 Mar 2018 cryptography's linked OpenSSL: OpenSSL 3.1.0 14 Mar 2018 Python's OpenSSL: OpenSSL 1.1.0h 27 Mar 2018 Python executable: C:\Users\Joe\AppData\Local\Programs\Python\Python37\python.exe Python version: 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] Platform: win32 sys.path: ['C:\\Users\\Joe\\Desktop\\Kindra', 'C:\\Users\\Joe\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip', 'C:\\Users\\Joe\\AppData\\Local\\Programs\\Python\\Python37\\DLLs', 'C:\\Users\\Joe\\AppData\\Local\\Programs\\Python\\Python37\\lib', 'C:\\Users\\Joe\\AppData\\Local\\Programs\\Python\\Python37', 'C:\\Users\\Joe\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages']
- 执行
python -c "import ssl; print(ssl.OPENSSL_VERSION)"结果:OpenSSL 1.1.0h 27 Mar 2018
更新OpenSSL操作
下载安装Win64 OpenSSL v1.1.1t,将安装包bin文件夹复制到C:\Users\Joe\AppData\Local\Programs\Python\Python37\Lib\site-packages\OpenSSL,并添加路径到环境变量,问题仍未解决。
解决方案
方案1:降级urllib3到兼容版本
urllib3 v2.0不再支持Python3.7及旧版OpenSSL,直接安装兼容的低版本即可:
pip install urllib3<2.0
执行后重新运行示例代码即可。
方案2:使用虚拟环境隔离依赖
创建并激活虚拟环境,再重新安装ChatterBot及兼容依赖:
# 创建虚拟环境 python -m venv chatterbot-env # 激活虚拟环境(Windows) chatterbot-env\Scripts\activate # 安装ChatterBot和兼容的urllib3 pip install chatterbot urllib3<2.0
之后在虚拟环境中运行示例代码。
方案3:升级Python版本(可选)
Python3.7已停止维护,升级到Python3.8+(自带更新的OpenSSL版本),可从根本解决依赖兼容问题,之后重新安装ChatterBot即可正常运行。
内容的提问来源于stack exchange,提问作者DeliriousJoe
相关产品推荐
相关产品推荐

