ImportError:没有名为bs4的模块(BeautifulSoup)

问题:ImportError:没有名为bs4的模块(BeautifulSoup)

我正在使用Python并使用Flask。当我在计算机上运行我的主Python文件时,它可以正常运行,但是当我激活venv并在终端中运行Flask Python文件时,它表示我的主Python文件具有“没有名为bs4的模块”。任何意见或建议,不胜感激。

I’m working in Python and using Flask. When I run my main Python file on my computer, it works perfectly, but when I activate venv and run the Flask Python file in the terminal, it says that my main Python file has “No Module Named bs4.” Any comments or advice is greatly appreciated.


回答 0

激活virtualenv,然后安装BeautifulSoup4:

$ pip install BeautifulSoup4

当您安装bs4使用easy_install,您在系统范围内进行了安装。因此,您的系统python可以导入它,但您的virtualenv python不能导入。如果您不需要bs4在系统python路径中安装,请卸载它并将其保留在virtualenv中。

有关virtualenvs的更多信息,请阅读此内容

Activate the virtualenv, and then install BeautifulSoup4:

$ pip install BeautifulSoup4

When you installed bs4 with easy_install, you installed it system-wide. So your system python can import it, but not your virtualenv python. If you do not need bs4 to be installed in your system python path, uninstall it and keep it in your virtualenv.

For more information about virtualenvs, read this


回答 1

对于python2.x

sudo pip install BeautifulSoup4

对于python3

sudo apt-get install python3-bs4

For python2.x:

sudo pip install BeautifulSoup4

For python3:

sudo apt-get install python3-bs4

回答 2

只需标记Balthazar的答案即可。跑步

pip install BeautifulSoup4

没有为我工作。改为使用

pip install beautifulsoup4

Just tagging onto Balthazar’s answer. Running

pip install BeautifulSoup4

did not work for me. Instead use

pip install beautifulsoup4

回答 3

pip3 install BeautifulSoup4

试试这个。这个对我有用。原因在这里得到了很好的解释..

pip3 install BeautifulSoup4

Try this. It works for me. The reason is well explained here..


回答 4

如果您将Anaconda用于软件包管理,则应执行以下操作:

conda install -c anaconda beautifulsoup4

If you are using Anaconda for package management, following should do:

conda install -c anaconda beautifulsoup4


回答 5

如果您使用Pycharm,请转到preferences - project interpreter - install bs4

如果尝试安装BeautifulSoup,它将仍然显示没有名为的模块bs4

If you use Pycharm, go to preferences - project interpreter - install bs4.

If you try to install BeautifulSoup, it will still show that no module named bs4.


回答 6

我建议您使用以下命令来卸载bs4库:

pip卸载bs4

然后使用以下命令进行安装:

须藤apt-get install python3-bs4

当我使用以下命令安装bs4库时,在Linux Ubuntu中遇到了相同的问题:

点安装bs4

I will advise you to uninstall the bs4 library by using this command:

pip uninstall bs4

and then install it using this command:

sudo apt-get install python3-bs4

I was facing the same problem in my Linux Ubuntu when I used the following command for installing bs4 library:

pip install bs4


回答 7

试试这个:

sudo python3 -m pip install bs4

Try this:

sudo python3 -m pip install bs4

回答 8

pip install --user BeautifulSoup4

pip install --user BeautifulSoup4


回答 9

pip3.7 install bs4

试试这个。它适用于python 3.7

pip3.7 install bs4

Try this. It works with python 3.7


回答 10

我做了@ rayid-ali所说的,除了我在Windows 10机器上,所以我省略了sudo。也就是说,我做了以下工作:

python3 -m pip install bs4

它就像一个pycharm。无论如何都像魅力一样工作。

I did what @rayid-ali said, except I’m on a Windows 10 machine so I left out the sudo. That is, I did the following:

python3 -m pip install bs4

and it worked like a pycharm. Worked like a charm anyway.


回答 11

最简单的是使用easy_install。

easy_install bs4 

如果pip失败,它将起作用。

The easiest is using easy_install.

easy_install bs4 

It will work if pip fails.


回答 12

很多针对Python 2编写的教程/参考资料都告诉您使用pip install somename。如果您使用的是Python 3,则要将其更改为pip3 install somename。

A lot of tutorials/references were written for Python 2 and tell you to use pip install somename. If you’re using Python 3 you want to change that to pip3 install somename.


回答 13

您可能想尝试使用安装bs4

pip install --ignore-installed BeautifulSoup4

如果上述方法不适合您。

You might want to try install bs4 with

pip install --ignore-installed BeautifulSoup4

if the methods above didn’t work for you.


回答 14

尝试重新安装模块,或者尝试使用以下命令与漂亮的汤一起安装

pip install --ignore-installed BeautifulSoup4

Try reinstalling the module OR Try installing with beautiful soup with the below command

pip install --ignore-installed BeautifulSoup4

回答 15

原始查询的附录: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命令。

Addendum to the original query: modules.py

help('modules')

$python modules.py

It lists that module bs4 already been installed.

_codecs_kr          blinker             json                six
_codecs_tw          brotli              kaitaistruct        smtpd
_collections        bs4                 keyword             smtplib
_collections_abc    builtins            ldap3               sndhdr
_compat_pickle      bz2                 lib2to3             socket

Proper solution is:

pip install --upgrade bs4

Should solve the problem.

Not only that, it will show same error for other modules as well. So you got to issue the pip command same way as above for those errored module(s).