Skip to content

ModuleNotFoundError: No module named 'x'

Error message

                No module named 'x'
              

Quick summary

Python can't find the module you're trying to import — it's not installed, not in the Python path, or the name is wrong.

Why this happens

  • The module is not installed in the current environment

  • Typo in the module name

  • Wrong virtual environment is activated

  • The module is not in PYTHONPATH or the current directory

Minimal example

✗ Broken code
import numpy  # ModuleNotFoundError if not installed
✓ Fixed code
# Run: pip install numpy
import numpy

numpy isn't installed in the current environment — install it with pip install numpy before importing.

How to diagnose

  • Run pip list to see if the module is installed

  • Verify the module name spelling against the import

  • Check which Python/venv is active with which python or python -V

How to fix

  • Install the module with pip install <module>

  • Fix the module name typo in the import statement

  • Activate the correct virtual environment before running

How to prevent

  • Use requirements.txt or pyproject.toml to track dependencies

  • Use virtual environments to manage installed packages per project

Related Resources

Related Lessons

Related Practice

← Back to language