Fix "ModuleNotFoundError: No module named" in Python
ModuleNotFoundError: No module named 'requests'
- ModuleNotFoundError: No module named 'cv2'
- ModuleNotFoundError: No module named 'src'
- ImportError: No module named requests
- E ModuleNotFoundError: No module named 'app'
Python cannot find the imported module in the interpreter that runs your script. In most cases the package is installed in a different Python (or not at all): install it with python -m pip install <package> inside the active virtual environment.
What this error means
When you run import, Python walks the sys.path list (script folder, standard library, the current interpreter’s site-packages) looking for the module. If it finds it nowhere, it raises ModuleNotFoundError, a subclass of ImportError introduced in Python 3.6. The error does not mean the package is missing from the machine: it means it is missing from that specific interpreter’s sys.path. That is why it often shows up when pip and python point to two different installations, when the virtual environment is not activated, or when the IDE uses a different interpreter than the terminal.
Common causes
- Package installed in another Python:
pipbelongs to Python 3.11 while the script runs on Python 3.13, orpipinstalled globally while the project uses a venv. - Virtual environment not activated: the package lives in
.venv, but the terminal, cron job or IDE runs the system Python. - Import name differs from the pip name:
import cv2comes fromopencv-python,PILfrompillow,sklearnfromscikit-learn,yamlfrompyyaml,bs4frombeautifulsoup4,dotenvfrompython-dotenv. - Local module outside
sys.path: you runpython src/app/main.pyinstead ofpython -m app.main, so the project root is not on the path andimport appfails.
Step-by-step fix
-
Identify the interpreter actually used
Print the path of the Python that runs your code and the one pip belongs to. Both must point to the same folder (ideally your
.venv).python -c "import sys; print(sys.executable)" python -m pip --version -
Install with
python -m pipCalling pip through
python -mguarantees it installs into the interpreter you are using. On Windows, thepylauncher does the same.# macOS / Linux python3 -m pip install requests # Windows py -m pip install requests -
Create and activate a virtual environment
One venv per project avoids version conflicts. Activate it before installing and before running the script.
# macOS / Linux python3 -m venv .venv source .venv/bin/activate python -m pip install -r requirements.txt # Windows (PowerShell) py -m venv .venv .venv\Scripts\Activate.ps1 python -m pip install -r requirements.txt -
Check the package name on PyPI
If
pip install cv2orpip install sklearnfails or installs something unexpected, look up the real distribution name. Always install the official package, not a lookalike.python -m pip install opencv-python # import cv2 python -m pip install pillow # import PIL python -m pip install scikit-learn # import sklearn python -m pip install pyyaml # import yaml python -m pip install python-dotenv # import dotenv -
Select the right interpreter in the IDE
In VS Code, open the command palette, run “Python: Select Interpreter” and pick the
.venvPython. In PyCharm: Settings, then Python Interpreter. In Jupyter, install from a cell with%pip install requeststo target the current kernel. -
Run your own modules as a package
For a local module, run from the project root with
-m, or install the project in editable mode so it is importable anywhere in the venv.# from the project root python -m app.main # or, with a pyproject.toml python -m pip install -e .
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Use one virtual environment per project and always run pip as
python -m pip. - List dependencies in
requirements.txtorpyproject.tomlsopip install -r requirements.txtrebuilds the environment identically. - In CI, Docker or cron, call the venv Python by its full path (
.venv/bin/python) rather thanpython.
Frequently asked questions
Why "No module named" when pip says "Requirement already satisfied"?
Because pip installed the package into a different interpreter than the one running your script. Compare python -c "import sys; print(sys.executable)" with python -m pip --version, then reinstall with python -m pip.
What is the difference between ModuleNotFoundError and ImportError?
ModuleNotFoundError means the module cannot be found. ImportError: cannot import name means the module exists but the requested name is not in it (typo, different version or circular import).
Why does it work in the terminal but not in VS Code?
VS Code uses its own selected interpreter, often the system Python. Pick your .venv one with “Python: Select Interpreter”.
Useful tools for this error
More Python errors
- Fix "IndentationError: unexpected indent" in Python
- Fix "'NoneType' object is not subscriptable" in Python
- Fix "error: externally-managed-environment" with pip
- Fix "UnicodeDecodeError: 'utf-8' codec can't decode byte" in Python
- Fix "KeyError" in Python (dict, pandas, os.environ)
- Fix "RecursionError: maximum recursion depth exceeded" in Python
- Fix "attempted relative import with no known parent package" in Python