Select Page

How to Solve Python Modulenotfounderror: no module named ‘bs4’

by | Programming, Python, Tips

A common error you may encounter when using Python is modulenotfounderror: no module named ‘bs4’.

This error occurs when the Python interpreter cannot detect the BeautifulSoup4 library in your current environment.

You can install BeautifulSoup4 in Python 3 with the command python3 -m pip install beautifulsoup4.

This tutorial goes through the exact steps to troubleshoot this error for the Windows, Mac and Linux operating systems.


ModuleNotFoundError: no module named ‘bs4’

What is BeautifulSoup?

Beautiful Soup is a Python library for extracting data from HTML and XML files.

This tutorial covers the installation of Beautiful Soup major version, or Beautiful Soup 4.

The simplest way to install BeautifulSoup4 is to use the package manager for Python called pip. The following installation instructions are for the major Python version 3.

How to Install BeautifulSoup4 on Windows Operating System

First, you need to download and install Python on your PC. Ensure you select the install launcher for all users and Add Python to PATH checkboxes. The latter ensures the interpreter is in the execution path. Pip is automatically on Windows for Python versions 2.7.9+ and 3.4+.

You can check your Python version with the following command:

python3 --version

You can install pip on Windows by downloading the installation package, opening the command line and launching the installer. You can install pip via the CMD prompt by running the following command.

python get-pip.py

You may need to run the command prompt as administrator. Check whether the installation has been successful by typing.

pip --version

BeautifulSoup4 installation on Windows Using pip

To install BeautifulSoup4, run the following command from the command prompt.

python3 -m pip install beautifulsoup4

We use python -m pip to execute pip using the Python interpreter we specify as Python. Doing this helps avoid ImportError when we try to use a package installed with one version of Python with a different version. You can use the command which python to determine which PYthon interpreter you are using.

How to Install BeautifulSoup4 on Mac Operating System using pip

Open a terminal by pressing command (⌘) + Space Bar to open the Spotlight search. Type in terminal and press enter. To get pip, first ensure you have installed Python3:

python3 --version
Python 3.8.8

Download pip by running the following curl command:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

The curl command allows you to specify a direct download link. Using the -o option sets the name of the downloaded file.

Install pip by running:

python3 get-pip.py

From the terminal, use pip to install BeautifulSoup4:

python3 -m pip install beautifulsoup4

How to Install BeautifulSoup4 on Linux Operating Systems

All major Linux distributions have Python installed by default. However, you will need to install pip. You can install pip from the terminal, but the installation instructions depend on the Linux distribution you are using. You will need root privileges to install pip. Open a terminal and use the commands relevant to your Linux distribution to install pip.

Installing pip for Ubuntu, Debian, and Linux Mint

sudo apt install python-pip3

Installing pip for CentOS 8 (and newer), Fedora, and Red Hat

sudo dnf install python-pip3

Installing pip for CentOS 6 and 7, and older versions of Red Hat

sudo yum install epel-release

sudo yum install python-pip3

Installing pip for Arch Linux and Manjaro

sudo pacman -S python-pip

Installing pip for OpenSUSE

sudo zypper python3-pip

BeautifulSoup4 installation on Linux with Pip

Once you have installed pip, you can install BeautifulSoup4 using:

python3 -m pip install beautifulsoup4

Installing BeautifulSoup4 Using Anaconda

First, to create a conda environment to install bs4.

conda create -n bs4 python=3.6 

Then activate the bs4 container. You will see “bs4” in parentheses next to the command line prompt.

source activate bs4

Now you’re ready to install BeautifulSoup4 using conda.

Anaconda is a distribution of Python and R for scientific computing and data science. You can install Anaconda by going to the installation instructions. Once you have installed Anaconda and created your conda environment, you can install BeautifulSoup4 using the following command:

conda install -c anaconda beautifulsoup4

Check BeautifulSoup4 Version

Once you have successfully installed BeautifulSoup4, you can check the version of it. If you used pip to install BeautifulSoup4, you can use pip show from your terminal.

python3 -m pip show beautifulsoup4
Name: beautifulsoup4
Version: 4.9.3
Summary: Screen-scraping library

Second, within your python program, you can import bs4 and then reference the __version__ attribute:

import bs4
print(bs4.__version__)
4.9.3

If you used conda to install BeautifulSoup4, you could check the version using the following command:

conda list -f beautifulsoup4
# Name                    Version                   Build  Channel
beautifulsoup4            4.9.3              pyhb0f4dca_0    anaconda

Using BeautifulSoup4

Let’s look at an example where we parse an HTML document using BeautifulSoup. First, we import the BeautifulSoup class. We will then read a URL using urllib to get an HTML document and assign it to a variable called content. Then, we create an object of the BeautifulSoup class with content as a parameter. The object represents the document as a nested data structure. We can call the find_all() method on the object to get the title of the HTML document. Let’s look at the code:

from bs4 import BeautifulSoup

from urllib.request import urlopen

url = "https://www.forbes.com"

content = urlopen(url).read()

soup = BeautifulSoup(content, 'html.parser')

print(soup.find_all("title")

When we run the code, we get:

[≺title itemprop="headline"≻Forbes≺/title≻]

Summary

Congratulations on reading to the end of this tutorial. The modulenotfounderror occurs if you misspell the module name, incorrectly point to the module path or do not have the module installed in your Python environment. If you do not have the module installed in your Python environment, you can use pip to install the package. However, you must ensure you have pip installed on your system. You can also install Anaconda on your system and use the conda install command to install BeautifulSoup4.

Go to the online courses page on Python to learn more about Python for data science and machine learning.

For further reading on missing modules in Python, go to the articles:

Have fun and happy researching!