ModuleNotFoundError: No module named ‘bs4’

Are you looking for the solution to the error “ModuleNotFoundError: No module named ‘bs4’“? Yes, you have come to the right place. Today I will show you how to solve this error.

The error “modulenotfounderror: no module named bs4″ occurs when we forget to install the “beautifulsoup4” module or install it in the wrong environment.

[Solution] modulenotfounderror: no module named bs4

To solve this error, you need to install the beautifulsoup4 python package using the pip install beautifulsoup command.

Here is the command to install BS4 in your system:

# If you are in a virtual environment or using Python 2
pip install beautifulsoup4

# For python 3, could also be pip3.10
pip3 install beautifulsoup4

# If you get permissions error you can run command as admin
sudo pip3 install beautifulsoup4
pip install beautifulsoup4 --user

# If you don't have pip in your PATH environment variable
python -m pip install beautifulsoup4

# For python 3 if you don't have pip in your PATH environment variable
python3 -m pip install beautifulsoup4

# Using py alias (Only works for Windows)
py -m pip install beautifulsoup4

# Alternative for Ubuntu/Debian
sudo apt-get install python3-bs4

# If you are coidng with Anaconda
conda install -c anaconda beautifulsoup4

# If you are using Jupyter Notebook
!pip install beautifulsoup4

When you successfully install BS4, you can import it like this:

from bs4 import BeautifulSoup

soup = BeautifulSoup(html_content)

print(soup.prettify())

The Python error “modulenotfounderror no module named ‘bs4’” can occur for multiple reasons:

  1. Not having beautifulsoup package installed. If you have not installed the bs4 package then you can install it with this command “pip install beautifulsoup”.
  2. Installing the beautifulsoup package in a different python version than you are using.
  3. Installing the package in the global environment when you are using the virtual environment.
  4. You are using the incorrect version of python on your IDE. (e.g. VS Code)
  5. Using the file name “bs4.py”. If you are using a name like this bs4.py then consider it change to something else.
  6. Declaring a variable with the name “bs4”. If you have a variable bs4 change its name to something else.

[Fixed]: Defaulting to user installation because normal site-packages is not writeable

Conclusion on “modulenotfounderror: no module named bs4

Programmers, we discussed the Python error “modulenotfounderror no module named ‘bs4’“. I hope you have solved your Python error. If you have any questions about the tutorial or are still facing the error, please let us know in the comments section.

Leave a Reply