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.
The first solution will be using pip package manager.
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.
pip install beautifulsoup4
pip3 install beautifulsoup4
sudo pip install beautifulsoup4
sudo pip3 install beautifulsoup4
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.
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
Use this command if you are using conda package manager.
conda install -c anaconda beautifulsoup4
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.
python setup.py install
Here is the command to install beautifulsoup4 via easy_install.
easy_install beautifulsoup4
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