Fix modulenotfound error no module named bs4 in Python

In today’s tutorial we’ll learn how to troubleshoot import errors related to the popular BeautifulSoup Python library.

In Python, you typically get the module not found errors, when attempting to use a 3rd party library without importing it first.

No module named bs4 error message in Jupyter, VsCode and Spyder

Below you can find screenshots of the error messages you will get when invoking the bs4 library without importing it fist.

Jupyter

Visual Studio Code (VScode)

Spyder

Solving the BeautifulSoup modulenotfound error

Using PIP – manually configured environments

If you aren’t using a pre-configured Python distribution such as Anaconda, invoking python package installer from the command prompt (in Windows) or Terminal (in macOS) will solve the problem.

  • First off, save any opened Python files or Jupyter notebooks.
  • Then, go ahead and shut down your development environment (either VSCode, Spyder, PyCharm, Jupyter Lab or Notebooks)..
  • Then open the Windows command prompt (Windows Key + R) and type cmd.
  • Now navigate to the path of your Python environment. Assuming that Python is installed in the Python310 directory, you’ll type the command below. Obviously replace the path to your environment as needed.
cd c:\Python310\Scripts
  • Hit Enter.
  • Next we’ll invoke the pip package manager as shown below:
pip install bs4
  • Hit Enter again; the bs4 package will be collected and installed.
  • Once finished, close the command box.
  • Open your Python development environment and import bs4.

Fix bs4 import errors on Anaconda and Mini Conda

If you are using the Anaconda distribution (or Mini Conda), then the process is just slightly different.

  • First off, save your work and shutdown your Python notebook.
  • Open the Anaconda Prompt (Windows Key +S, then type Anaconda and hit Run as Administrator).
  • Then type the following command:
conda activate <path_to_your_anaconda_installation>
  • If you are unsure about your Anaconda environment path, you can easily find it from the Anaconda Prompt:
conda info --envs
  • Then install the packages using the conda install command
conda install bs4
  • The install package will be collected. Then, when asked whether to proceed, hit Y and hit Enter.
  • Once the install is done, close the Anaconda Command Prompt.
  • Next, open your development environment and import the bs4 package.
from bs4 import BeautifulSoup

Additional Learning