ModuleNotFoundError:没有名为"bs4"的模块,但在Python 3.8.5上使用PIP3正确安装



对不起,我知道这个问题出现了一点,但我被难住了。以前工作良好的代码现在不再工作。我已经尝试过卸载和重新安装python 3.8.5,设置新的环境,将代码剥离回基本内容。我已经安装和卸载了BS4和Beautifulsoup4几十次了。

我的方法是pip3 install bs4

安装进行得很好,但当我运行代码时,我会得到

File "/Users/rupertdenton/Desktop/Coding/Anybody/tester.py", line 1, in <module>
from bs4 import BeautifulSoup
ModuleNotFoundError: No module named 'bs4'

我三次检查了python版本,它是3.8.5,并成功重新安装:

Requirement already satisfied: soupsieve>1.2 in ./venv/lib/python3.8/site-packages (from beautifulsoup4->bs4) (2.0.1)
Installing collected packages: beautifulsoup4, bs4
Successfully installed beautifulsoup4-4.9.1 bs4-0.0.1

但是代码仍然抛出相同的错误。这是大约一周前还可以正常工作的精简代码。

from bs4 import BeautifulSoup
def scrapecafes(city, area):
#url = 'https://www.broadsheet.com.au/melbourne/guides/best-cafes-thornbury' #go to the website
url = f"https://www.broadsheet.com.au/{city}/guides/best-cafes-{area}"
response = requests.get(url, timeout=5)
soup_cafe_names = BeautifulSoup(response.content, "html.parser")
type(soup_cafe_names)
cafeNames = soup_cafe_names.findAll('h2', attrs={"class":"venue-title", }) #scrape the elements
cafeNamesClean = [cafe.text.strip() for cafe in cafeNames]

如果您的代码取决于环境,则需要首先激活环境。

. env/bin/activate

如果env是环境的名称。

然后

pip3 install bs4 requests

然后运行您的程序。

最新更新