我正在尝试使用Python学习web抓取,并使用学习已安装的请求
pip3 install requests
并使用相同的命令再次进行检查。我得到了以下输出:
Requirement already satisfied: requests in /usr/local/lib/python3.6/dist-packages
但当我尝试执行以下代码时:
from bs4 import BeautifulSoup
import requests
with open('/home/aca/Documents/Python files/test.html') as html_file:
soup = BeautifulSoup(html_file, 'lxml')
print(soup.prettify())
在Atom版本1.26.1中,我收到了以下错误消息:
Traceback (most recent call last):
File "/home/aca/Documents/Python files/experiment.py", line 2, in <module>
import requests
ImportError: No module named requests
[Finished in 0.167s]
我通过使用Pythonshell导入请求,确认了在IDLE中安装请求,并且在没有ImportError的情况下完成了任务。
我陷入了困境。这是旧版本Atom的问题吗?如果是,请为lubuntu 18.04推荐一个轻量级ide。
尝试检查您的Python版本。很有可能在一个解释器中安装了Request,而Atom使用另一个解释器。
直接在命令行中运行脚本。
您安装了多个版本的Python吗?
试试这个:
python3.6 -m pip install requests
您还可以在运行此experiment.py
脚本时验证Atom是否选择了正确的解释器。
如果正常的pip3命令不起作用,请尝试
sudo python3 -m pip install requests
我做了
python -m pip install requests
这将把requests
模块添加到安装python版本的位置。
然后使用命令在视图中导入请求模块.py
import requests