- ModuleNotFoundError: No module named ‘bs4’ in Python
- Reason: ‘bs4’ Module Not Installed in Python
- Solution (For Windows): Install the ‘bs4’ Module in Python
- How to Uninstall the beautifulsoup4 / bs4 Module?
- Install the beautifulsoup4 / bs4 Module in Anaconda
- Install the beautifulsoup4 / bs4 Module in Jupyter Notebook
- Solution (For Linux): Install ‘bs4’ Module in Python
- Conclusion
- How to fix ModuleNotFoundError: No module named ‘bs4’
- How to reproduce the error
- How to fix this error
- Install commands for other environments
- Other common causes for this error
- Handling multiple versions of Python
- Handling Python virtual environment
- Handle IDE using a different Python version
- Conclusion
- Take your skills to the next level ⚡️
- About
- Search
- Modulenotfounderror: no module named bs4 : Best Solution
- Modulenotfounderror: no module named bs4 ( Solution ) –
- Solution 1: Installation of beautifulsoup4 via pip –
- For Python 2. x version series –
- For Python 3. x version series –
- With Admin Rights
- With Version Specification
- If pip is not configured properly –
- Solution 2: Installation of beautifulsoup4 via conda-
- Solution 3: Installation of beautifulsoup4 via source code –
- Solution 3: Installation of beautifulsoup4 via easy_install –
- Solution 4: Installation of beautifulsoup4 in Linux Family Platform-
- Join our list
ModuleNotFoundError: No module named ‘bs4’ in Python
Python provides a “bs4” module which is also referred to as “beautifulsoup4” to pull out data from HTML and XML files. Programmers also use the Python “bs4” module for web scraping. To access the “bs4” module, you must download and import it at the program’s start. The interpreter will give a “no module named bs4” error when the user imports the module without being installed in Python.
This write-up will give you various reasons and solutions for the error “no module named bs4” in Python. The below-given points are followed in this article:
Reason: ‘bs4’ Module Not Installed in Python
In Python, the “no module named bs4” error appears when the “bs4” package/module is imported into a program without being installed. The below snippet shows the “bs4” module error:
The above snippet shows the “bs4” module error because the “beautifulsoup4” or “bs4” is accessed in the program without being installed in Python.
Solution (For Windows): Install the ‘bs4’ Module in Python
To resolve this “ModuleNotFoundError,” we need to install the “beautifulsoup4” module using the “pip” package manager. Follow this dedicated article to download and install the “pip” package manager.
Step 1: Launch cmd
To open the “cmd” terminal in windows, press the (Windows Key + R) and type the “cmd” in the run dialog box.
Hit the “OK” button to launch the Command Prompt.
Step 2: Install the beautifulsoup4 / bs4 Module
After opening the cmd terminal, type the below command to install the “beautifulsoup4” module in Python:
> pip install beautifulsoup4
The above snippet shows the “bs4” module installation in the command prompt terminal.
Suppose the above command generates a permission error. In that case, you can execute the below command to avoid that error:
> pip install beautifulsoup4 --user
Note: Please make sure to install the “selenium” module in the correct Python version. Because sometimes, the IDE, such as thorny in our case, creates the virtual environment with a different Python version. In such a case, installing the module globally in cmd might not resolve the problem. We need to install the module in the IDE’s shell, also.
Step 3: Verify the beautifulsoup4 / bs4 Module
To verify the “beautifulsoup4” module, you can use the below command:
The above snippet shows the name, version, and location of our installed “beautifulsoup4” module.
How to Uninstall the beautifulsoup4 / bs4 Module?
To uninstall the module from Python windows, you can type the following command in the terminal:
> pip uninstall beautifulsoup4
The above snippet shows that the “bs4” module was successfully uninstalled from Python.
Install the beautifulsoup4 / bs4 Module in Anaconda
Executing the below command will install the bs4 in the Anaconda environment:
> conda install -c anaconda beautifulsoup4
Install the beautifulsoup4 / bs4 Module in Jupyter Notebook
The given below piece of code is used to install the “bs4” module in Jupyter Notebook:
> !pip install beautifulsoup4
Solution (For Linux): Install ‘bs4’ Module in Python
To resolve this error in Python Linux you can use the “pip” package manager or “apt” command. Visit the following articles to install “pip” in Linux or Ubuntu 22.04:
The first option to install the “bs4” module is using the following pip command:
$ sudo pip install beautifulsoup4
The alternative approach to download and install the “bs4” module is by using “apt” command:
$ sudo apt install python3-bs4
Note: Error also arises when your IDE using an incorrect version of Python or the module was installed globally but not in a virtual environment. You can simply follow the above solutions in your working IDE shell to resolve all these errors.
That’s how the “bs4” module’s error can be fixed in Python.
Conclusion
The “No module named bs4” error appears in Python when the user tries to import the “bs4” module into a program without being installed in Python. To resolve this module not found error, you can install the “bs4” module using the “pip” and “apt” commands in Linux and Windows. This Python post presented a detailed guide on resolving the no module named “bs4” error.
How to fix ModuleNotFoundError: No module named ‘bs4’
One error that you might encounter when working with Python is:
This error occurs when Python can’t find the bs4 module in your current Python environment.
This tutorial shows examples that cause this error and how to fix it.
How to reproduce the error
Suppose you want to use the Beautiful Soup 4 library to work with HTML and XML files.
You import the BeautifulSoup class from the bs4 module in your code as follows:
"But you get the following error when running the code:
This error occurs because the bs4 module is not a built-in Python module, so you need to install it before using it.
How to fix this error
To resolve this error, you need to install the beautifulsoup4 library using pip as shown below:
Once the module is installed, you should be able to run the code that imports bs4 without receiving this error.
Install commands for other environments
The install command might differ depending on what environment you used to run the Python code.
Here’s a list of common install commands in popular Python environments:
Once the module is installed, you should be able to run the code without receiving this error.
Other common causes for this error
If you still see the error even after installing the module, it means that the bs4 module can’t be found in your Python environment.
- You may have multiple versions of Python installed on your system, and you are using a different version of Python than the one where Beautiful Soup is installed.
- You might have Beautiful Soup installed in a virtual environment, and you are not activating the virtual environment before running your code.
- Your IDE uses a different version of Python from the one that has Beautiful Soup
Let’s see how to fix these errors in practice.
Handling multiple versions of Python
If you have multiple versions of Python installed on your system, you need to make sure that you are using the specific version where the Beautiful Soup module is available.
You can test this by running the which -a python or which -a python3 command from the terminal:
In the example above, there are two versions of Python installed on /opt/homebrew/bin/python3 and /usr/bin/python3 .
Suppose you run the following steps in your project:
- Install Beautiful Soup with pip using /usr/bin/ Python version
- Install Python using Homebrew, you have Python in /opt/homebrew/
- Then you run from bs4 import BeautifulSoup in your code
The steps above will cause the error because Beautiful Soup is installed in /usr/bin/ , and your code is probably executed using Python from /opt/homebrew/ path.
To solve this error, you need to run pip install beautifulsoup4 command again so that Beautiful Soup is installed and accessible by the new Python version.
Next, you can also have Beautiful Soup installed in a virtual environment.
Handling Python virtual environment
Python venv package allows you to create a virtual environment where you can install different versions of packages required by your project.
If you are installing beautifulsoup4 inside a virtual environment, then the module won’t be accessible outside of that environment.
You can see if a virtual environment is activated or not by looking at your command prompt.
When a virtual environment is activated, the name of that environment will be shown inside parentheses as shown below:
In the picture above, the name of the virtual environment (base) appears when the Conda virtual environment is activated.
You need to turn off the virtual environment so that pip installs to your computer.
When your virtual environment is created by Conda, run the conda deactivate command. Otherwise, running the deactivate command should work.
Handle IDE using a different Python version
Finally, the IDE from where you run your Python code may use a different Python version when you have multiple versions installed.
For example, you can check the Python interpreter used in VSCode by opening the command palette ( CTRL + Shift + P for Windows and ⌘ + Shift + P for Mac) then run the Python: Select Interpreter command.
You should see all available Python versions listed as follows:
You need to use the same version where you installed Beautiful Soup so that the module can be found when you run the code from VSCode.
Once done, you should be able to import Beautiful Soup into your code.
Conclusion
In summary, the ModuleNotFoundError: No module named ‘bs4’ error occurs when the beautifulsoup4 library is not available in your Python environment. To fix this error, you need to install beautifulsoup4 using pip .
If you already have the module installed, make sure you are using the correct version of Python, deactivate the virtual environment if you have one, and check for the Python version used by your IDE.
By following these steps, you should be able to import BeautifulSoup class from the bs4 module in your code successfully.
I hope this tutorial is helpful. Until next time! 👋
Take your skills to the next level ⚡️
I’m sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I’ll send new stuff straight into your inbox!
About
Hello! This website is dedicated to help you learn tech and data science skills with its step-by-step, beginner-friendly tutorials.
Learn statistics, JavaScript and other programming languages using clear examples written for people.
Search
Type the keyword below and hit enter
Modulenotfounderror: no module named bs4 : Best Solution
Modulenotfounderror: no module named bs4 occurs if the beautifulsoup4 python module is not properly installed in the python environment. We use beautifulsoup4 for HTML data extraction, and web scraping which is quite rapid in most content-oriented application. Typically we use some code where we import beautifulsoup4 but we forgot the prerequisite to install the library and we get this error. Apart from it since we can maintain multiple python versions in the system altogether. We install the package like beautifulsoup4 but in a different interpreter/version.
Hence the root cause for the no module named bs4 error is misconfiguration of beautifulsoup4 package or improper installation of beautifulsoup4. As a solution, we can reinstall and configure the respective package. So let’s begin.
Modulenotfounderror: no module named bs4 ( Solution ) –
The first solution will be using pip package manager.
Solution 1: Installation of beautifulsoup4 via pip –
We can install beautifulsoup4 via with single command. But we need to take care below points which slightly variate on the basis of different scenarios.
For Python 2. x version series –
pip install beautifulsoup4
For Python 3. x version series –
pip3 install beautifulsoup4
With Admin Rights
sudo pip install beautifulsoup4
sudo pip3 install beautifulsoup4
With Version Specification
The above command will install the latest version for beautifulsoup4 but in case your requirement is for a specific version then you can specify like below.
pip3 install beautifulsoup4==4.10.0
Here is the release history for beautifulsoup4. You can go through it if there is any version-specific requirement for you.
If pip is not configured properly –
Typically Python installers set the path for pip in the system path but in case there is some issue you can try the below command for the same as well.
python -m pip install beautifulsoup4
python3 -m pip install beautifulsoup4
Solution 2: Installation of beautifulsoup4 via conda-
Use this command if you are using conda package manager.
conda install -c anaconda beautifulsoup4
Solution 3: Installation of beautifulsoup4 via source code –
The above two ways are simpler than installation from the source code. It is also not too difficult but we need to download the code and install the dependencies using requirement.txt and finally, we need to run the below command.
Solution 3: Installation of beautifulsoup4 via easy_install –
Here is the command to install beautifulsoup4 via easy_install.
easy_install beautifulsoup4
Solution 4: Installation of beautifulsoup4 in Linux Family Platform-
This is very specific to Linux Family OS. You can try apt-get for beautifulsoup4 installation. This will fix up the modulenotfounderror.
sudo apt-get install python3-bs4
Thanks
Data Science Leaner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
We respect your privacy and take protecting it seriously
Thank you for signup. A Confirmation Email has been sent to your Email Address.