Main Ecosystems
HomePython CorePandas ReferenceNumPy ScientificFastAPI & PydanticDjango Enterprise
More Ecosystems
Environment & SetupRequests & HTTPAsyncIO ConcurrencyObject-Oriented OOPPyTorch Deep LearningScikit-Learn MLFlask FrameworkWeb ScrapingDatabase & ORMDevOps & Docker

ModuleNotFoundError: No module named bs4 but in pip list is already installed

Verified FixPython 3.10+BeautifulSoup4 4.12+Silo: scraping

Quick Fix / Solution Rapide

Install BeautifulSoup using the exact Python executable running your script: python -m pip install beautifulsoup4 or align your IDE interpreter.

Root Cause Analysis

This error occurs when a developer sees beautifulsoup4 in pip list, but running a script raises ModuleNotFoundError: No module named 'bs4', caused by an environment misalignment where pip belongs to a different Python interpreter than the one executing the script.

1. Multiple Python Interpreters and Virtual Environment Desynchronization

Systems frequently have multiple Python installations (system Python, Homebrew, Conda, Pyenv, and project .venv). If pip installs packages into global Python while the terminal or IDE runs inside .venv, the active interpreter cannot see the library.

2. Package Name vs Import Name Discrepancy

The package name on PyPI is beautifulsoup4 (or legacy bs4), but the import statement in Python code is from bs4 import BeautifulSoup. Installing the deprecated pip install BeautifulSoup (BeautifulSoup 3) fails on Python 3.

3. IDE Interpreter Settings in VS Code or PyCharm

VS Code may select a global interpreter in the bottom status bar while your terminal has activated a virtual environment.

4. Shebang Line Misalignment

Scripts starting with #!/usr/bin/python bypass active virtual environments and invoke the outdated system interpreter.

Reproduction Code (MCVE)

Example: Bug Reproduction
# **Note de reproductibilité :** Dépendant de la configuration des venvs.
raise ModuleNotFoundError("No module named 'bs4' (interpreter /usr/bin/python3 does not match pip target /opt/homebrew/bin/pip3)")

Solution 1: Install via python -m pip to Guarantee Interpreter Alignment

Always invoke pip via python -m pip so the package is installed into the exact environment of the active python binary.

Example: Recommended Solution
import sys

print(f'Active Python Executable: {sys.executable}')
print('Command to execute in terminal:')
print(f'{sys.executable} -m pip install beautifulsoup4')

Solution 2: Verify and Import BeautifulSoup Correctly

Verify that bs4 is accessible and import BeautifulSoup.

Example: Alternative Solution
try:
    from bs4 import BeautifulSoup
    print(f'BeautifulSoup imported successfully!')
except ImportError:
    print('Run: pip install beautifulsoup4')

A common mistake is running pip install bs4 instead of pip install beautifulsoup4. While bs4 is a dummy package that redirects to beautifulsoup4, directly installing beautifulsoup4 guarantees the latest release. Edge cases occur in Jupyter Notebooks: running !pip install bs4 inside a notebook cell installs into the shell's default Python, which may differ from the notebook's active kernel. Use %pip install beautifulsoup4 in notebooks. Contrast this error with ImportError: cannot import name 'BeautifulSoup' from 'bs4', which indicates a shadowed local bs4.py script.