Fix ModuleNotFoundError in VS Code: 5 Causes and Fixes
ModuleNotFoundError in VS Code almost always means the wrong Python interpreter is selected, not a missing package. Here are the 5 causes and exact fixes: interpreter mismatch, wrong venv, missing __init__.py, and more.
VS Code runs Python through a specific interpreter. If that interpreter is not your virtual environment, or the package is not installed in it, you get ModuleNotFoundError even when pip install looked like it worked.
The error is almost never about a missing package. It is almost always about which Python is running your code.
Step 1: Check Which Python VS Code Is Using
Open Command Palette (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on Mac) and run Python: Select Interpreter.
The interpreter shown in the bottom status bar is what VS Code uses to run your files. If it shows /usr/bin/python3 or any system Python instead of .venv/bin/python, that is the problem.
Fix: Select the interpreter that points to your virtual environment. Look for one with
.venvorvenvin the path.
Step 2: Verify the Package Is Installed in the Right Environment
Warning: Running
pip installwithout activating the venv installs into your system Python. VS Code's venv interpreter will not find it. Always activate before installing.
Step 3: Fix Missing init.py for Local Modules
If the error is about your own code and not a third-party package, the directory is probably not set up as a Python package:
Add __init__.py to make utils/ a proper Python package:
Then run from the project root, not from inside a subdirectory:
Step 4: Check for Editable Install Issues
If you are developing a package and importing it by name:
Without -e, changes to your source files do not reflect in the installed package. With -e, Python imports directly from your source directory.
Step 5: VS Code Settings Override
Sometimes VS Code has a python.defaultInterpreterPath hardcoded in .vscode/settings.json that overrides your selection:
Fix: Update the path to your venv, or delete the setting and use the Command Palette to select the interpreter manually.
Diagnostic Checklist
Run these in order before changing anything else:
Common Causes
| Symptom | Cause | Fix |
|---|---|---|
| Package installed but error persists | Wrong interpreter in VS Code | Select the correct .venv interpreter |
pip install ran, still fails | Installed to system Python, not venv | Activate venv, reinstall |
| Error on your own module | Missing __init__.py | Add empty __init__.py |
| Works in terminal, fails in VS Code | VS Code using different Python | Check status bar interpreter |
| Works locally, fails in CI | Different environment | Check requirements.txt is complete |
FAQ
Q: Why does the correct interpreter show in the status bar but the error still appears?
A: VS Code may have cached the old interpreter. Reload the window with Ctrl+Shift+P then Developer: Reload Window and try again.
Q: I have multiple venvs in the same project. Which one should I pick?
A: The one where you installed your dependencies. Run pip list inside each to check. If you are unsure, delete them and create a fresh one at the project root.
For ModuleNotFoundError in complex monorepos or projects with custom package structure, paste the full error and your directory tree into DebugAI and it will identify whether the problem is interpreter mismatch, missing package, or broken import path.
Debug faster starting today.
Free VS Code extension. 10 sessions/day. No credit card.