ModuleNotFoundError

Python SystemImport ErrorCommonLast updated: June 29, 2026Tested on:CPython v3.12PIP v24.0June 2026

This error occurs when Python cannot locate the specified module package inside your active environment search paths.

ModuleNotFoundError Quick Fix⏱️ Est. Fix Time: 2 minutes

Usually happens because:

  • The third-party package has not been installed in the environment
  • Active virtual environment (venv) is not activated in the shell terminal
  • Spelling typo or capitalization discrepancy in import string

🔍 Quick Checklist:

What is ModuleNotFoundError?

The 'ModuleNotFoundError' is a subclass of 'ImportError' introduced in Python 3.6. It indicates that the Python interpreter failed to locate an external library or a local module referenced in an 'import' statement. This typically happens because the package has not been installed inside the active virtual environment, the module folder lacks an '__init__.py' file (for older Python compatibility), or the folder is missing from Python's search path ('sys.path').

Common Causes

  • Missing package installation: Trying to import a third-party library (like 'requests' or 'numpy') that has not been installed using pip.
  • Active virtual environment mismatch: Installing the package globally but running the code inside an isolated virtual environment (venv) where it is missing.
  • Spelling typo or casing mismatch: Mismatch in capitalization or spelling of the module name.
CauseFrequency
Package has not been installed using pip⭐⭐⭐⭐⭐
Virtual environment mismatch (stale shell environment)⭐⭐⭐⭐
Folder path omitted from sys.path listing⭐⭐⭐

Common Mistakes

  • Installing package using global system commands (`sudo apt install python3-requests`) when working within local virtual environments (`venv`), which will not propagate dependencies inside the environment.
  • Using spelling name matches that differ from installation naming (e.g. running `pip install PyYAML` but writing `import yaml` in scripts).

How to Fix

1Install the package using pip: Install the missing module (e.g. 'pip install requests') in your active environment.
2Verify active virtual environment: Ensure your terminal shell has activated the correct environment (e.g. 'source venv/bin/activate').
3Verify sys.path location: Add the parent directory of your local module to sys.path programmatically or via environment variables.

Python Operations & Verification

Ensure your virtual environment is active, then download the missing package using pip.

Virtual Environment Pip Install Example
# 1. Activate your virtual environment (Linux/macOS)
$ source venv/bin/activate

# 2. Install the package inside the active environment
(venv) $ pip install requests

# 3. Launch python script successfully
(venv) $ python script.py

Platform Specific Fixes

Verify installed packages inside the system python path.

Linux Config
python3 -m pip list | grep -i requests

Best Practices

  • Manage repository package configurations inside a `requirements.txt` file and execute installs using `pip install -r requirements.txt`.
  • Always check that your shell prompt shows the active environment marker `(venv)` before editing packages.

Frequently Asked Questions (FAQ)

Q: What is ModuleNotFoundError?

It is a subclass of ImportError raised when a module cannot be found during import resolution.

Q: Why does it fail when I already installed the package?

You likely installed the package globally or in a different virtual environment. Make sure you activate your current virtual environment before installing packages.

Q: How do I import a local file from a parent folder?

Add the folder path to the environment variable PYTHONPATH, or append it to sys.path in your script: 'import sys; sys.path.append("/path/to/folder")'.

Q: Why is the package name different from the import name?

Some packages have different installation names compared to their import names. For example, you run 'pip install beautifulsoup4' but import it using 'import bs4'.

Still having this problem?

Didn't solve your problem?

SuggestRequest Error