Public

How to Fix "No module named" in a Python venv

This article contains ads and affiliate links.

Python Python
t-salad t-salad committed bf4a319

When working inside a Python virtual environment (venv), you may hit this error:

Sponsored Links
ModuleNotFoundError: No module named 'requests'

It means Python cannot find the module you imported. Inside a venv the cause is almost always a mismatch between the Python that has the package installed and the Python that is actually running your script. This guide walks through the causes from most to least common.

The most reliable fix first

Install the package using the same interpreter that runs your code, with python -m pip. This avoids installing into the wrong Python:

python -m pip install requests

Then confirm which interpreter and pip you are using:

python -c "import sys; print(sys.executable)"
python -m pip -V

If both paths point inside your venv folder, the package will be visible to your script.

Cause 1: The virtual environment is not activated

If the venv is not active, your system Python runs instead, and it does not have the packages you installed in the venv. Activate it first:

macOS / Linux

source venv/bin/activate

Windows (PowerShell)

.\venv\Scripts\Activate.ps1

When active, the venv name appears at the start of your prompt, e.g. (venv).

Cause 2: The package is not installed in this venv

List what is actually installed in the active environment:

python -m pip list

If the package is missing, install it (inside the activated venv):

python -m pip install requests
Sponsored Links

Cause 3: You are running the wrong Python

If several Python installations exist, an IDE, a python vs python3 difference, or an editor's run button can launch a different interpreter than the one where you installed the package. Check the interpreter path at runtime:

import sys
print(sys.executable)

If this path is not inside your venv, point your editor/IDE at the venv interpreter, or run the script with the venv's Python explicitly.

Cause 4: A project-structure / import path issue

If the missing module is your own code (not a pip package), the problem is usually the import path. For a layout like this:

my_project/
├── venv/
├── src/
│   ├── __init__.py
│   ├── main.py
│   └── utils.py
└── requirements.txt

Import using the package path from the project root, and run from the project root:

# main.py
from src.utils import my_function

if __name__ == "__main__":
    my_function()

Summary

  • Install with python -m pip install ... so the package lands in the interpreter that runs your code.
  • Activate the venv first; an inactive venv falls back to system Python.
  • Confirm the interpreter with python -c "import sys; print(sys.executable)".
  • For your own modules, fix the import path and run from the project root.

References

Sponsored Links
Sponsored Links
★ Share on X
Related posts