运行《Python编程:从入门到实践》17.4.1的代码时会报错:ImportError: No module named OpenSSL(同时发现cryptography模块也找不到)
#《Python编程:从入门到实践》17.4.1节代码
import requests
# 执行API调用并存储响应
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
r = requests.get(url)
print("Status code:", r.status_code)
# 将API响应存储在一个变量中
response_dict = r.json()
# 处理结果
print(response_dict.keys())
原因:未安装OpenSSL模块。
解决方法:使用pip安装OpenSSL模块,打开命令窗口,输入pip install OpenSSL命令安装。VS环境下还要进入Python Environments页面(在解决方案资源管理器里右键Python Environments,点击View All Python Environments,白色下拉框中选择IntelliSense)待IntelliSense分析完成后,点击 Refresh DB 按钮。
(安装时可以看到,cryptography模块也一起安装了)