ModuleNotFoundError: No module named 'x' means the Python interpreter that ran your code could not find a module called x anywhere on its search path. Usually the package isn't installed for that particular interpreter, so install it with python -m pip install <package> using the same python you run the script with, inside your virtual environment if you use one.
The full error looks like this (real output, Python 3.12):
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'requestz'
Check the spelling first. requestz here is a typo for requests, and no install will fix that.
Cause 1: the package isn't installed for this Python
Most machines have more than one Python: a system one, one from python.org or the Microsoft Store, Homebrew's on macOS, and one per virtual environment. Each has its own set of installed packages. Running plain pip install can install into a different Python from the one that runs your script.
The fix is to call pip through the interpreter itself. The pip documentation explains that python -m pip runs pip using the Python interpreter you specified as python:
python -m pip install requests
On Windows with the Python launcher, use py -m pip install requests. On macOS and Linux, if your command is python3, use python3 -m pip install requests.
To see which interpreter you are using and which one pip belongs to:
python -c "import sys; print(sys.executable)"
python -m pip --version
Example output on Windows:
C:\Program Files\Python312\python.exe
pip 26.0.1 from C:\Users\alex\AppData\Roaming\Python\Python312\site-packages\pip (python 3.12)
If pip --version (without python -m) names a different Python version or path, that is your problem. On recent Debian and Ubuntu releases, installing into the system Python is blocked with error: externally-managed-environment; see how to fix externally-managed-environment.
Cause 2: the virtual environment isn't active (or is a different one)
A virtual environment has its own packages, separate from the global ones. A package installed globally is invisible inside the venv, and the other way round. This test shows it: yaml was installed globally on the test machine, but a fresh venv can't see it:
python -m venv .venv
.venv\Scripts\python -c "import yaml"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'yaml'
Activate the environment, then install into it:
# Windows
.venv\Scripts\activate
# macOS / Linux
source .venv/bin/activate
python -m pip install pyyaml
To check whether you are inside a venv, run python -c "import sys; print(sys.prefix != sys.base_prefix)"; it prints True in a virtual environment. If a project has a requirements.txt, install everything at once with python -m pip install -r requirements.txt. More in Python virtual environments with venv.
Cause 3: the install name differs from the import name
The name you pip install is the distribution name on PyPI. The name you import is the module inside it, and they often differ. Installing the import name either fails or installs an unrelated package.
| Error says | Install this |
|---|---|
No module named 'yaml' | pyyaml |
No module named 'cv2' | opencv-python |
No module named 'PIL' | pillow |
No module named 'sklearn' | scikit-learn |
No module named 'bs4' | beautifulsoup4 |
No module named 'dateutil' | python-dateutil |
No module named 'dotenv' | python-dotenv |
These pairs were checked with importlib.metadata.packages_distributions() on Python 3.12, which maps each importable top-level module to the distribution that provides it. Run it yourself to find the package behind any module that is already installed somewhere:
python -c "import importlib.metadata as m; print(m.packages_distributions().get('cv2'))"
['opencv-python']
Cause 4: importing your own code from the wrong folder
The same error appears for your own modules, and the cause is where Python starts looking. The Python tutorial explains that sys.path starts with the directory containing the script being run, then PYTHONPATH, then the installation's default paths including site-packages.
So with this layout:
project/
pkg/
__init__.py
helper.py
main.py # contains: from pkg import helper
running python pkg/main.py from project/ fails, because Python searches project/pkg/, not project/:
ModuleNotFoundError: No module named 'pkg'
Run it as a module from the project root instead. With -m, Python puts the current directory at the start of the search path, so pkg is found:
python -m pkg.main
For larger projects, make the project installable (a pyproject.toml) and install it in editable mode with python -m pip install -e ., so imports work from anywhere in that environment.
Cause 5: a file that shadows the module
Because the script's own folder is searched first, a file named after a real module takes its place. A script called yaml.py or requests.py in your folder will be imported instead of the library, even though the library is installed. A test script named yaml.py that runs import yaml fails with AttributeError: partially initialized module 'yaml' has no attribute 'safe_load' (most likely due to a circular import). Rename your file and delete any __pycache__ folder next to it.
If it still fails
- Works in the terminal, fails in the editor: the editor uses another interpreter. In VS Code run Python: Select Interpreter from the Command Palette and pick the project's
.venv. - Works for you, fails in cron, a service or Docker: those run a different Python with a different environment. Use the venv's full interpreter path, such as
/home/alex/project/.venv/bin/python script.py. - Jupyter: the notebook kernel may not be the Python you installed into. Install from a notebook cell with
%pip install package, which targets the running kernel. - Module only exists on newer Python: standard-library modules such as
tomllib(added in 3.11) don't exist on older versions; checkpython --version.
Read the last line of the traceback for the missing name and the lines above it for which file tried to import it; how to read a stack trace covers this in detail.
Frequently asked questions
- What is the difference between ModuleNotFoundError and ImportError?
- ModuleNotFoundError is a subclass of ImportError, added in Python 3.6. It means the module itself could not be found. A plain ImportError, such as cannot import name 'x' from 'y', means the module was found but the name you asked for isn't in it, often because of a version mismatch or a circular import.
- Why does it work in the terminal but not in VS Code or PyCharm?
- The editor is running a different Python interpreter from your terminal. In VS Code, open the Command Palette and run Python: Select Interpreter, then choose your project's .venv. In PyCharm, set the interpreter under Settings, Project, Python Interpreter.
- pip says the package is installed but Python can't find it. Why?
- pip installed it for a different Python. Compare python -c "import sys; print(sys.executable)" with python -m pip --version, which prints the interpreter pip belongs to. Install with python -m pip install so both use the same interpreter.
- Should I fix it by editing sys.path or PYTHONPATH?
- Only as a last resort. Adding paths by hand hides the real problem and breaks when the project moves. Install third-party packages into the environment, and run your own code as a module from the project root (python -m package.module) or install it with pip install -e .