Skip to content
Commitpedia
Log in Join & edit

Fix: ModuleNotFoundError: No module named ‘…’

Python cannot find a module you import. Usually the package was installed into a different interpreter or virtual environment than the one running your code.

Edit history of Fix: ModuleNotFoundError: No module named ‘…’: 1 recorded edit
Edit history · 1 edit
Type
Article
Edits
1
Editors
1
First issued
15 Oct 2026
Last edit
15 Oct 2026
Reading
2 min

The full message looks like this:

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'

It means: the Python interpreter that is running right now searched every folder on its import path and found no module with that name. The key words are “right now”. Your machine may have several Pythons (system, Homebrew, python.org installer, one per virtual environment), and each has its own packages.

Step 1: find out which Python is running

python -c "import sys; print(sys.executable)"
python -m pip --version    # shows which Python this pip belongs to

If the paths point to different installations, you have found the problem: the package went into one Python, your code runs in another.

Step 2: install into that exact interpreter

Activate your project’s virtual environment, then install with python -m pip so pip is guaranteed to match the interpreter:

source .venv/bin/activate      # Windows: .venv\Scripts\activate
python -m pip install requests
python main.py

If you do not have a virtual environment yet, create one first with python3 -m venv .venv. Plain pip install on a system Python is often blocked with externally-managed-environment on recent Linux and macOS setups anyway.

Other common causes

  • Your editor uses another interpreter. In VS Code, run “Python: Select Interpreter” and choose the one inside .venv. PyCharm has the same setting under the project interpreter.
  • The install name differs from the import name. You install beautifulsoup4 but import bs4; Pillow is imported as PIL, PyYAML as yaml, scikit-learn as sklearn, python-dotenv as dotenv. Check the package’s documentation.
  • A typo or wrong case. Module names are case-sensitive on most systems: import Requests fails.
  • Your own module is not on the path. If from utils import x fails, check where you run the command. Python adds the folder of the script you run (not your shell’s current folder) to the path. For a package layout, run modules from the project root with python -m mypackage.main instead of python mypackage/main.py.
  • Tests cannot import your code. Run python -m pytest instead of pytest, or set pythonpath = ["."] under [tool.pytest.ini_options] in pyproject.toml.
  • A cron job or service uses the system Python. Call the environment’s interpreter by full path, for example /home/me/project/.venv/bin/python /home/me/project/main.py.

A related but different error

If you named your own file after a library (say requests.py or random.py), the import succeeds but loads your file, and you get AttributeError or ImportError: cannot import name ... instead. Rename your file and delete any __pycache__ folder next to it.

Prevent it next time

  1. Create a virtual environment for every project.
  2. Always install with python -m pip install ....
  3. Record dependencies in requirements.txt or pyproject.toml so a fresh environment can be rebuilt in one command.

See also

  • 0001 Virtual environments with venv A virtual environment is a private folder of Python packages for one project, so projects never fight over versions. Create one with… Beginner · Concept · Any system Article Environments & packages 15 Oct 2026
  • 0008 Fix: TypeError: ‘NoneType’ object is not subscriptable You used [ ] on a value that is None. Something you expected to return a list, dict or match returned nothing.… Beginner · Fix · Any system Article Common errors 15 Oct 2026
  • 0011 Bootstrap a new Python project with a venv A Bash script that creates a project folder with a virtual environment, .gitignore, requirements.txt and a starter main.py, then runs it to… Intermediate · How-to · Linux, macOS Script Environments & packages 15 Oct 2026
  • 0073 Fix: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) Python's json module found nothing it could parse at the very first character. The input is usually empty, HTML, or already consumed.… Beginner · Fix · Any system Article Common errors · CSV & JSON 15 Oct 2026
  • 0261 Build a Python command-line to-do app: from empty folder to installable command Create a small to-do tool in Python with argparse and JSON storage, test it with pytest and package it with pyproject.toml so… Beginner · Tutorial · Any system Article Environments & packages · Files & automation · Testing 15 Oct 2026

Repos in the wild

  • uv: Python package and project manager astral-sh/uv uv is a very fast Python package and project manager written in Rust. It replaces pip, venv and related tools, manages lock files and can install Python itself. GitHub
  • Ruff: Python linter and formatter astral-sh/ruff Ruff is a fast Python linter and code formatter written in Rust. It catches bugs such as undefined names and unused imports before you run your code. GitHub

History

Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.

  1. commitpedia-admin +3,013 chars

Report a problem

Something wrong, unsafe or copied? Tell the editors.
What is wrong?