ModuleNotFoundError: No module named 'bs4'

Posted in Python by Dirk - last update: Feb 05, 2024

Python raises the ModuleNotFoundError: No module named 'bs4 when it is unable to find the BeautifulSoup library. The most likely cause is that you didn’t install BeautifulSoup in the environment where you are running your code. Quick fix: install BeautifulSoup using: the pip install beautifulsoup4 command

Possible causes and solutions

BeautifulSoup is a Python library for pulling data out of HTML and XML files. It provides Pythonic idioms for iterating, searching, and modifying the parse tree. BeautifulSoup sits on top of popular Python parsers like lxml and html5lib, allowing you to try out different parsing strategies or trade speed for flexibility.

Sample code:

from bs4 import BeautifulSoup

html_doc = """
<html>
  <head>
    <title>Sample HTML Page</title>
  </head>
  <body>
    <div id="content">
      <h1>Heading 1</h1>
      <p>This is a sample paragraph.</p>
      <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
      </ul>
    </div>
  </body>
</html>
"""

# Create a Beautiful Soup object
soup = BeautifulSoup(html_doc, 'html.parser')

# Extract data
title = soup.title.text
paragraph = soup.p.text
list_items = [item.text for item in soup.ul.find_all('li')]

# Display extracted data
print(f'Title: {title}')
print(f'Paragraph: {paragraph}')
print(f'List Items: {list_items}')

And when trying to run the code you get the ’no module named error':

Traceback (most recent call last):
  File "myfile.py", line 1, in <module>
ModuleNotFoundError: No module named 'bs4'

Luckily, it is easy to fix. Below you can find the most common causes and how to fix them:

‘bs4’ Library is not installed

In your code you are importing the BeautifulSoup module from bs4

from bs4 import BeautifulSoup

which generates the ModuleNotFoundError: No module named 'bs4'. The the most common cause for this error: the environment where you are running your code doesn’t have the bs4 (Beautifulsoup) library installed.

You can use the following command in the terminal or command prompt to check if the bs4 library is installed:

pip show bs4

If bs4 is not installed you will get this result:

WARNING: Package(s) not found: bs4

Solution:

pip install beautifulsoup4

‘bs4’ Module installed in the Wrong Directory

It is possible that you did install the BeautifulSoup module, but that the directory where you installed it is not in the Python path.

You can check the Python path in a few different ways:

1. Using sys module in Python:

Open a Python script or the Python interpreter and run the following code:

import sys
print(sys.path)

This will print a list of directories that make up the Python path.

2. Using Command Line

Open a command prompt or terminal and run:

  • Windows
echo %PYTHONPATH%
  • Unix/Linux/macOS
echo $PYTHONPATH

If the PYTHONPATH environment variable is set, it will display the directories in the Python path.

Solution: Move the bs4 module to a directory that is included in the Python path or add the BeautifulSoup module’s directory to the sys.path using sys.path.append().

‘bs4’ not installed in Virtual Environment

Even if you have BeautifulSOup installed in on your computer, if you are using a virtual environment you cannot access it from this environment.

If you are using virtual environments, ensure that the virtual environment is activated, and that the bs4 modules is installed within this environment.

Installation is simple:

pip install beautifulsoup4

File Naming Conflicts:

It is not really likely - but if you happen to have a bs4.py file in your environment and you also installed the standard bs4 library it will cause a conflict. Rename your script/module to avoid conflicts with existing module names.

Other articles