Skip to content
FR
  1. LuzineDebug
  2. Python
  3. ModuleNotFoundError: No module named 'requests'
Python Beginner ≈ 5 min to fix

Fix "ModuleNotFoundError: No module named" in Python

Error message
ModuleNotFoundError: No module named 'requests'
  • ModuleNotFoundError: No module named 'cv2'
  • ModuleNotFoundError: No module named 'src'
  • ImportError: No module named requests
  • E ModuleNotFoundError: No module named 'app'
In short

Python cannot find the imported module in the interpreter that runs your script. In most cases the package is installed in a different Python (or not at all): install it with python -m pip install <package> inside the active virtual environment.

Be the first to confirm this fix

What this error means

When you run import, Python walks the sys.path list (script folder, standard library, the current interpreter’s site-packages) looking for the module. If it finds it nowhere, it raises ModuleNotFoundError, a subclass of ImportError introduced in Python 3.6. The error does not mean the package is missing from the machine: it means it is missing from that specific interpreter’s sys.path. That is why it often shows up when pip and python point to two different installations, when the virtual environment is not activated, or when the IDE uses a different interpreter than the terminal.

Common causes

  1. Package installed in another Python: pip belongs to Python 3.11 while the script runs on Python 3.13, or pip installed globally while the project uses a venv.
  2. Virtual environment not activated: the package lives in .venv, but the terminal, cron job or IDE runs the system Python.
  3. Import name differs from the pip name: import cv2 comes from opencv-python, PIL from pillow, sklearn from scikit-learn, yaml from pyyaml, bs4 from beautifulsoup4, dotenv from python-dotenv.
  4. Local module outside sys.path: you run python src/app/main.py instead of python -m app.main, so the project root is not on the path and import app fails.

Step-by-step fix

  1. Identify the interpreter actually used

    Print the path of the Python that runs your code and the one pip belongs to. Both must point to the same folder (ideally your .venv).

    bash
    python -c "import sys; print(sys.executable)"
    python -m pip --version
  2. Install with python -m pip

    Calling pip through python -m guarantees it installs into the interpreter you are using. On Windows, the py launcher does the same.

    bash
    # macOS / Linux
    python3 -m pip install requests
    
    # Windows
    py -m pip install requests
  3. Create and activate a virtual environment

    One venv per project avoids version conflicts. Activate it before installing and before running the script.

    bash
    # macOS / Linux
    python3 -m venv .venv
    source .venv/bin/activate
    python -m pip install -r requirements.txt
    
    # Windows (PowerShell)
    py -m venv .venv
    .venv\Scripts\Activate.ps1
    python -m pip install -r requirements.txt
  4. Check the package name on PyPI

    If pip install cv2 or pip install sklearn fails or installs something unexpected, look up the real distribution name. Always install the official package, not a lookalike.

    bash
    python -m pip install opencv-python   # import cv2
    python -m pip install pillow          # import PIL
    python -m pip install scikit-learn    # import sklearn
    python -m pip install pyyaml          # import yaml
    python -m pip install python-dotenv   # import dotenv
  5. Select the right interpreter in the IDE

    In VS Code, open the command palette, run “Python: Select Interpreter” and pick the .venv Python. In PyCharm: Settings, then Python Interpreter. In Jupyter, install from a cell with %pip install requests to target the current kernel.

  6. Run your own modules as a package

    For a local module, run from the project root with -m, or install the project in editable mode so it is importable anywhere in the venv.

    bash
    # from the project root
    python -m app.main
    
    # or, with a pyproject.toml
    python -m pip install -e .

Did this fix work for you?

Confirm it in one click: it will rise for the next developers stuck on the same error.

Keep it from coming back

  • Use one virtual environment per project and always run pip as python -m pip.
  • List dependencies in requirements.txt or pyproject.toml so pip install -r requirements.txt rebuilds the environment identically.
  • In CI, Docker or cron, call the venv Python by its full path (.venv/bin/python) rather than python.

Frequently asked questions

Why "No module named" when pip says "Requirement already satisfied"?

Because pip installed the package into a different interpreter than the one running your script. Compare python -c "import sys; print(sys.executable)" with python -m pip --version, then reinstall with python -m pip.

What is the difference between ModuleNotFoundError and ImportError?

ModuleNotFoundError means the module cannot be found. ImportError: cannot import name means the module exists but the requested name is not in it (typo, different version or circular import).

Why does it work in the terminal but not in VS Code?

VS Code uses its own selected interpreter, often the system Python. Pick your .venv one with “Python: Select Interpreter”.

Useful tools for this error

More Python errors

All Python errors →