我正在使用 Python 并使用 Flask。当我在我的计算机上运行我的主要 Python 文件时,它运行良好,但是当我激活 venv 并在终端中运行 Flask Python 文件时,它说我的主要 Python 文件有“没有名为 bs4 的模块”。非常感谢任何意见或建议。
22 回答
激活 virtualenv,然后安装 BeautifulSoup4:
$ pip install BeautifulSoup4
当您使用 安装bs4
时easy_install
,您在系统范围内安装了它。所以你的系统 python 可以导入它,但不是你的 virtualenv python。如果你不需要bs4
安装在你的系统python路径下,卸载它并保留在你的virtualenv中。
有关 virtualenvs 的更多信息,请阅读此
对于python2.x:
sudo pip install BeautifulSoup4
对于python3:
sudo apt-get install python3-bs4
只是标记到巴尔萨扎的答案。跑步
pip install BeautifulSoup4
对我不起作用。而是使用
pip install beautifulsoup4
尝试这个:
sudo python3 -m pip install bs4
pip3 install BeautifulSoup4
尝试这个。这个对我有用。原因在这里很好解释..
如果您使用 Anaconda 进行包管理,则应执行以下操作:
conda install -c anaconda beautifulsoup4
如果您使用Pycharm,请转到preferences - project interpreter - install bs4
.
如果您尝试安装BeautifulSoup,它仍然会显示没有名为bs4
.
我会建议您使用以下命令卸载 bs4 库:
pip卸载bs4
然后使用以下命令安装它:
sudo apt-get install python3-bs4
当我使用以下命令安装 bs4 库时,我在我的 Linux Ubuntu 中遇到了同样的问题:
点安装bs4
这对我有用。
pipenv pip install BeautifulSoup4
我一直在互联网上广泛搜索。
我正在使用 Python 3.6 和 MacOS。我已经卸载并安装了,pip3 install bs4
但是没有用。似乎 python 无法检测或搜索bs4
模块。
这是有效的:
python3 -m pip install bs4
该-m
选项允许您添加模块名称。
我做了@rayid-ali 所说的,除了我在 Windows 10 机器上,所以我省略了 sudo。也就是说,我做了以下事情:
python3 -m pip install bs4
它就像一个pycharm。无论如何,它就像一个魅力。
发现答案与 BeautifulSoup 无关,真是太烦人了。对于 linux 用户,运行命令“python”不同,版本 2 退出,您可能忘记更改 python 的 bash 文件别名。因此,您可能已经使用 3 创建了虚拟环境
python3 -m venv .venv
因此,要安装东西,请使用
python3 -m pip install beautifulsoup4
不是
pip install beautifulsoup4
东西安装在不同的版本中,你对发生的事情摸不着头脑。
pip install --user BeautifulSoup4
许多教程/参考资料都是为 Python 2 编写的,并告诉您使用 pip install somename。如果您使用的是 Python 3,您希望将其更改为 pip3 install somename。
pip3.7 install bs4
尝试这个。它适用于 python 3.7
另一种解决方案PyCharm
:
转到File -> Settings -> Python Interpreter
,单击plus
符号并找到beautifulsoup4
。
单击install
。
原始查询的附录: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 命令。
如果您落后于公司代理,请尝试使用以下命令
pip install --proxy=http://www-YOUR_PROXY_URL.com:PROXY_PORT BeautifulSoup4
更好的方法是(“-U”:将所有软件包升级到最新的可用版本。):
$ python3 -m pip install -U pip && python3 -m pip install -U bs4
或从 apt(Debian、Ubuntu、Mint 等)安装:
$ sudo apt install python3-bs4
最简单的是使用easy_install。
easy_install bs4
如果 pip 失败,它将起作用。
尝试重新安装模块或尝试使用以下命令安装漂亮的汤
pip install --ignore-installed BeautifulSoup4
您可能想尝试安装 bs4
pip install --ignore-installed BeautifulSoup4
如果上述方法对您不起作用。