Verify that the Python interpreter executing your script matches the exact environment where the package is installed (python -m pip install <package>), confirm your virtual environment is activated, and check sys.path for working-directory or file-shadowing issues—because ImportError: No module named ... (or ModuleNotFoundError in Python 3.6+) occurs whenever Python cannot locate the target module in any directory listed in sys.path.

Basic Diagnostic & Fix Workflow

# 1. Always install packages using the exact Python binary you run scripts with
python -m pip install <package_name>

# 2. Verify which Python interpreter is active and where its site-packages live
python -c "import sys; print(sys.executable); print(sys.path)"

# 3. Confirm the package is installed in that specific environment
python -m pip show <package_name>

Common Root Causes & Targeted Fixes

  • 1. Mismatched python and pip executables (multiple Python versions): Running pip install requests may install into Python 3.11’s site-packages, while python app.py runs Python 3.12. Always pair them via python -m pip install <package> (or py -3.12 -m pip install <package> on Windows) so both commands target the exact same interpreter.
  • 2. Virtual environment not activated (or wrong interpreter selected in IDE): Installing a package inside .venv and running your script globally—or vice versa—triggers ModuleNotFoundError.
    • Terminal: Activate the virtual environment first (source .venv/bin/activate on Linux/macOS, or .venv\Scripts\Activate.ps1 on Windows PowerShell).
    • VS Code / PyCharm: Open the Command Palette (Ctrl+Shift+P / Cmd+Shift+P) → Python: Select Interpreter → choose the interpreter inside your project’s .venv folder.
  • 3. PyPI distribution name differs from the import name: Installing a package succeeds, yet importing it fails because the module name on disk differs from the PyPI package name (for example, pip install beautifulsoup4 is imported as from bs4 import BeautifulSoup, and pip install scikit-learn is imported as import sklearn).
  • 4. Local file or folder shadowing a library name: Naming your own script requests.py, email.py, json.py, or pandas.py places it first in sys.path[0], causing Python to import your local file instead of the installed library. Rename your local file and delete any stale __pycache__ folder.
  • 5. Running a script inside a package subdirectory (sys.path root issue): If you have a project structure like project/src/utils.py and project/tests/test_app.py, running python tests/test_app.py sets sys.path[0] to project/tests/, making from src import utils fail. Run scripts as modules from the project root instead:
    # Run from the root 'project/' directory using -m
    python -m tests.test_app
    

Root Cause & Solution Reference

Root Cause Diagnostic Symptom Solution
pip / python interpreter mismatch pip list shows the package, but python -m pip list does not Run python -m pip install <package> to install into the active interpreter.
Inactive virtual environment sys.executable points to system Python instead of .venv Activate .venv in your shell or select it in your IDE’s interpreter picker.
Local script name collision module.__file__ points to your project directory instead of site-packages Rename your local .py file so it does not shadow a built-in or third-party module.
Relative / parent package import failure Importing a sibling or parent folder fails when running a nested script Run from the project root with python -m package.module or install the project in editable mode (pip install -e .).
Different import vs. package name pip install <name> succeeds, import <name> fails Check the library’s documentation for its actual top-level module name (e.g., Pillow → PIL, PyYAML → yaml).