ImportError:没有名为 bs4 的模块(BeautifulSoup)[重复]
- 2025-03-18 08:54:00
- admin 原创
- 49
问题描述:
我正在使用 Python 和 Flask。当我在计算机上运行主 Python 文件时,它运行良好,但当我激活 venv 并在终端中运行 Flask Python 文件时,它显示我的主要 Python 文件有“没有名为 bs4 的模块”。任何评论或建议都非常感谢。
解决方案 1:
激活虚拟环境,然后安装 BeautifulSoup4:
$ pip install beautifulsoup4
当您使用bs4
进行安装时easy_install
,您会在系统范围内安装它。因此您的系统 python 可以导入它,但不能导入您的虚拟环境 python。如果您不需要bs4
将其安装在系统 python 路径中,请卸载它并将其保留在您的虚拟环境中。
有关虚拟环境的更多信息,请阅读
解决方案 2:
对于python2.x:
sudo pip install BeautifulSoup4
对于python3:
sudo apt-get install python3-bs4
解决方案 3:
只是附在 Balthazar 的答案上。正在运行
pip install BeautifulSoup4
对我来说不起作用。相反,使用
pip install beautifulsoup4
解决方案 4:
尝试一下:
sudo python3 -m pip install bs4
解决方案 5:
我已经在互联网上广泛搜索了。
我使用的是 Python 3.6 和 MacOS。我已卸载并安装了,pip3 install bs4
但没有用。似乎 Python 无法检测或搜索bs4
模块。
这是有效的:python3 -m pip install bs4
该-m
选项允许您添加模块名称。
https://docs.python.org/3/using/cmdline.html
解决方案 6:
pip3 install BeautifulSoup4
试试这个。它对我有用。原因在这里解释得很好。
解决方案 7:
发现答案与 BeautifulSoup 无关真是令人恼火。对于 Linux 用户,运行命令“python”时,版本 2 会退出,您可能忘记更改 python 的 bash 文件别名。因此,您可能使用 3 创建了虚拟环境
python3 -m venv .venv
因此,要安装东西,请使用
python3 -m pip install beautifulsoup4
不是
pip install beautifulsoup4
安装的东西有不同的版本,您可能搞不清楚到底发生了什么。
解决方案 8:
如果您使用Pycharm,请转到preferences - project interpreter - install bs4
。
如果您尝试安装BeautifulSoup,它仍会显示没有名为 的模块bs4
。
解决方案 9:
如果您使用 Anaconda 进行包管理,请执行以下操作:
conda install -c anaconda beautifulsoup4
解决方案 10:
我建议您使用以下命令卸载 bs4 库:
pip 卸载 bs4
然后使用此命令进行安装:
sudo apt-get 安装 python3-bs4
当我在 Linux Ubuntu 中使用以下命令安装 bs4 库时遇到了同样的问题:
pip安装bs4
解决方案 11:
这对我有用。
pipenv pip install BeautifulSoup4
解决方案 12:
我按照 @rayid-ali 所说的做了,只不过我在 Windows 10 机器上,所以我省略了 sudo。也就是说,我做了以下事情:
python3 -m pip install bs4
并且它像 pycharm 一样工作。无论如何,它都运行良好。
解决方案 13:
pip3.7 install bs4
试试这个。它适用于 Python 3.7
解决方案 14:
pip install --user BeautifulSoup4
解决方案 15:
我也遇到了这个问题Python 3
。就我而言,我可以通过运行以下命令解决它:
$ pip3 install BeautifulSoup4
而不是:
$ pip install BeautifulSoup4
解决方案 16:
许多教程/参考资料都是针对 Python 2 编写的,并告诉您使用 pip install somename。如果您使用的是 Python 3,则需要将其更改为 pip3 install somename。
解决方案 17:
更好的方法是(“-U”:将所有包升级到最新可用版本。):
$ python3 -m pip install -U pip && python3 -m pip install -U bs4
或者从 apt 安装(Debian、Ubuntu、Mint 等):
$ sudo apt install python3-bs4
解决方案 18:
最简单的方法是使用easy_install。
easy_install bs4
如果 pip 失败,它将会起作用。
解决方案 19:
你可能想尝试使用以下方式安装 bs4
pip install --ignore-installed BeautifulSoup4
如果上述方法对您不起作用。
解决方案 20:
尝试重新安装模块或尝试使用以下命令使用 beautiful soup 安装
pip install --ignore-installed BeautifulSoup4
解决方案 21:
原始查询的附录:modules.py
help('modules')
$python modules.py
它列出了已安装的模块 bs4。
_codecs_kr blinker json six
_codecs_tw brotli kaitaistruct smtpd
_collections bs4 keyword smtplib
_collections_abc builtins ldap3 sndhdr
_compat_pickle bz2 lib2to3 socket
正确的解决方案是:
pip install --upgrade bs4
应该可以解决问题。
不仅如此,它还会对其他模块显示相同的错误。因此,您必须以与上述相同的方式对这些错误模块发出 pip 命令。
解决方案 22:
如果您使用公司代理,请尝试使用以下命令
pip 安装 --proxy=http://www-YOUR_PROXY_URL.com:PROXY_PORT BeautifulSoup4
解决方案 23:
还有一个解决方案PyCharm
:
前往File -> Settings -> Python Interpreter
,单击plus
标志并查找beautifulsoup4
。
点击install
。
解决方案 24:
您需要在 PATH 环境变量中包含 pip。如果没有,请使用此链接:python -m pip install beautifulsoup4
这对我来说很管用。希望对你也一样管用。
解决方案 25:
就我而言,什么时候做
pip install beautifullsoap4
总是安装到我的python3.9包中,我的
python3 --version
给出了输出,因为Python 3.10.6
我通过升级 pip 并正确指向而3.10
不是3.9
然后一切都按预期进行
解决方案 26:
阅读完所有答案后,对我有用的是添加--clean
选项pyinstaller
。
例子:
pyinstaller --onefile --console --clean --name Email_Parser main.py
我尝试过但没有效果的方法:
删除并重新创建虚拟环境
python -m pyinstaller
在虚拟环境中安装 pyinstaller
扫码咨询,免费领取项目管理大礼包!