Unable to import beautiful soup

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

from bs4 import BeautifulSoup

I get an error: ImportError: cannot import name BeautifulSoup

import bs4 gives no errors.

I also tried import bs4.BeautifulSoup and just imported bs4 and created a BeautifulSoup object with: bs4.BeautifulSoup()

Any guidance would be appreciated.

+10
source share
7 answers

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

Thanks to everyone who helped!

+10
source

I solved this by setting beautifulsoup4, "4" is important.

 pip install beautifulsoup4 
+3
source

I have experienced a change in this problem and am posting for the benefit of others.

I named my example Python script bs4.py

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

from bs4 import BeautifulSoup , an ImportError was thrown, but the confusing (for me) import worked fine from an interactive shell in the same venv environment.

After renaming the Python script, the import works as expected. The error was caused by Python trying to import itself from a local directory rather than using a system copy of bs4

+1
source

Copy bs4 and beautifulsoup4-4.6.0.dist-info from C: \ python \ Lib \ site-packages to the local directory of the project. It worked for me. Here Python is actually looking for the library in the local directory, and not in the place where it was installed!

+1
source

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

+1
source

I also encountered this type error at the beginning, even after installing all the necessary modules, including installing pip bs4 (if you installed it, there is no need to install beautifusoup4 | BeautifulSoup4 via pip or anywhere else, it comes with bs4 )

Solution: just go to the python file in which it is installed, C:\python\Lib\site-packages and then copy bs4 and beautifulsoup4-4.6.0.dist-info and paste it into your project folder where you saved your work project.

0
source

The best way to solve this problem is when creating an interpreter to choose the global path to Python in your system (/usr/local/bin/python3.7). Make sure that in pycharm python shell --version displays as 3.7. This should not show 2.7

-1
source

Source: https://habr.com/ru/post/986088/


All Articles