Cannot import Beautiful Soup

2020-08-10 08:45发布

问题:

I am trying to use BeautifulSoup, and despite using the import statement:

from bs4 import BeautifulSoup

I am getting the error: ImportError: cannot import name BeautifulSoup

import bs4 does not give any errors.

I have also tried import bs4.BeautifulSoup and just importing bs4 and creating a BeautifulSoup object with: bs4.BeautifulSoup()

Any guidance would be appreciated.

回答1:

The issue was I named the file HTMLParser.py , and that name is already used somewhere in the bs4 module.

Thanks to everyone that helped!



回答2:

I experienced a variation of this problem and am posting for others' benefit.

I named my Python example script bs4.py

Inside this script, whenever trying to import bs4 using the command:

from bs4 import BeautifulSoup, an ImportError was thrown, but confusingly (for me) the import worked perfectly from an interactive shell within the same venv environment.

After renaming the Python script, imports work as expected. The error was caused as Python tries to import itself from the local directory rather than using the system copy of bs4



回答3:

I solved it by installing beautifulsoup4, the "4" is essential.

pip install beautifulsoup4


回答4:

Make sure the directory from which you are running your script does not contain a filename called bs4.py.



回答5:

I found out after numerous attempts to solve the ImportError: cannot import name 'BeautifulSoup4' that the package is actually called BeautifulSoup so the import should be:

from bs4 import BeautifulSoup



回答6:

Copy bs4 and beautifulsoup4-4.6.0.dist-info from C:\python\Lib\site-packages to your local project directory. It worked for me. Here, python actually looks for the library in local directory rather than the place where the library was installed!



回答7:

One of the possible reason: If you have more than one python versions installed and let's say you installed beautifulsoup4 using pip3, it will only be available for import when you run it in python3 shell.



回答8:

I was also facing this type error in the beginning even after install all the modules which were required including pip install bs4 (if you have installed this then no need to install beautifusoup4 | BeautifulSoup4 through pip or anywhere else it comes with bs4 itself)

Solution : Just go to your python file where it is installed C:\python\Lib\site-packages and then copy bs4 and beautifulsoup4-4.6.0.dist-info folders and paste it to your project folder where you have saved your working project.



回答9:

There is no problem with package just need to Copy bs4 and beautifulsoup4-4.6.0.dist-info into your project directory



回答10:

The bs4 and beautifulsoup4 folders might be in the site-packages folder. So copy BeautifulSoup4 folder in bs4 and then try the below code. It worked for me.

from bs4 import BeautifulSoup

Since you were importing BeautifulSoup from bs4 and in bs4 there was no BeautifulSoup folder. That is why it was showing ImportError: cannot import name BeautifulSoup.



回答11:

The best way to resolve is, while creating your interpreter select your global python path on your system(/usr/local/bin/python3.7). Make sure that in pycharm shell, python --version appears as 3.7. It shouldn't show 2.7