Beautiful Soup is a package for parsing HTML and XML documents in Python. If you use the package and get in trouble with the error “ModuleNotFoundError: No module named ‘bs4′”, keep reading on. This article will give you a few methods to fix the error. Let’s move on.
The reason for “ModuleNotFoundError: No module named ‘bs4′”
The ModuleNotFoundError: No module named ‘bs4’ error is raised when you try to import a module that is not installed in your Python environment. The following example will cause the error
Code:
import requests from bs4 import BeautifulSoup URL = "https://learnshareit.com/how-to-convert-a-string-to-a-tuple-without-splitting-in-python/" page = requests.get(URL) soup = BeautifulSoup(page.content, "html.parser") # Find the title of the article title = soup.find("h1", class_="entry-title") print("Title:", title.text) # Take all header h3 from the article header = soup.find_all("h3") for i in header: if (i.span != None): print("Header:", i.text)
Result:
Traceback (most recent call last):
line 2, in <module>
from bs4 import BeautifulSoup
ModuleNotFoundError: No module named 'bs4'
Note that our website’s backend has been installed the bs4 module, so you can run the code without errors. In case your system was installed the module, you will see the error as above.
Next, we will provide you with a few methods to install the bs4 package.
Solution to “ModuleNotFoundError: No module named ‘bs4′”
You must install the bs4 package before using it. To install the package, following one of three methods below.
Install the bs4 package
To fix this error, you need to install the bs4 module using pip. pip is the package manager for Python, allowing you to install and manage Python packages (like bs4).
To install bs4, open a terminal window and run the following command:
pip install bs4
Install the beautifulsoup4 package
The bs4 is a dummy package that the developers manage to prevent name squatting. In case you install the bs4 unsuccessfully, you can install the official version, which is beautifulsoup4 package instead.
This will install the bs4 module and make it available in your Python code.
To install the beautifulsoup4, run the following command:
pip install beautifulsoup4
Install beautifulsoup in Anaconda
Alternatively, if you use Anaconda as the base environment, you can install the beautifulsoup package in Anaconda by the below command:
conda install -c anaconda beautifulsoup4
By one of the methods above, you can install the bs4 module and crawl data from a website. Now, we will run the piece of code and see the result:
Code:
import requests from bs4 import BeautifulSoup URL = "https://learnshareit.com/how-to-convert-a-string-to-a-tuple-without-splitting-in-python/" page = requests.get(URL) soup = BeautifulSoup(page.content, "html.parser") # Find the title of the article title = soup.find("h1", class_="entry-title") print("Title:", title.text) # Take all header h3 from the article header = soup.find_all("h3") for i in header: if (i.span != None): print("Header:", i.text)
Result:
Title: How To Convert A String To A Tuple Without Splitting In Python
Header: Use the split() function
Header: Use commas at the end
Summary
In summary, the error “ModuleNotFoundError: No module named ‘bs4′” in Python occurs because you use the bs4 module that was not installed in the Python environment. You can install the bs4 module or the official version-beautifulsoup4 module to fix the error. Also, using Anaconda as the base environment, you can install beautifulsoup by the conda syntax.

My name is Robert Collier. I graduated in IT at HUST university. My interest is learning programming languages; my strengths are Python, C, C++, and Machine Learning/Deep Learning/NLP. I will share all the knowledge I have through my articles. Hope you like them.
Name of the university: HUST
Major: IT
Programming Languages: Python, C, C++, Machine Learning/Deep Learning/NLP