The red traceback ends with something like ModuleNotFoundError: No module named 'pandas'. The message looks straightforward, but blindly running pip install again can make the situation more confusing when several Python installations are present.
First identify whether the missing name is a third-party package, one of your own files, or a package whose install name differs from its import name. Then apply the matching fix below.
Fix 1 — Confirm the exact Python executable running your file
Run this inside the same VS Code terminal you use for the project:
python -c "import sys; print(sys.executable)"
Compare the result with the interpreter displayed in VS Code’s status bar. If they differ, select the correct interpreter and open a fresh terminal. Existing terminals can remain attached to an older environment.
Fix 2 — Install the package through that interpreter
python -m pip install package-name
For example:
python -m pip install pandas
Confirm where it was installed:
python -m pip show pandas
Using python -m pip is safer than plain pip because it explicitly connects pip to the Python executable you just checked.
Fix 3 — Activate the correct virtual environment
Windows:
.venv\Scriptsctivate
macOS or Linux:
source .venv/bin/activate
Then install and test:
python -m pip install package-name
python -c "import package_name"
If your terminal prompt shows the environment name but VS Code still runs another interpreter, select the .venv interpreter explicitly.
Fix 4 — Check the package name versus the import name
The name used with pip is not always the name used in Python. Common examples include installing opencv-python but importing cv2, installing pillow but importing PIL, and installing scikit-learn but importing sklearn.
Read the package documentation before changing the import statement. Guessing the module name is an efficient way to install three packages and fix none of them.
Fix 5 — Make sure your own module is in the project
Suppose your code says:
from helpers import clean_data
Python must be able to find helpers.py or a helpers package from the current working directory. Open the whole project folder in VS Code and run the command from the project root.
Check the current directory:
python -c "import os; print(os.getcwd())"
If the script is launched from the wrong folder, relative imports can fail even though the files are present.
Fix 6 — Avoid file names that shadow real packages
Do not name your own file pandas.py, requests.py, json.py, random.py, or another module you import. Python may load your local file instead of the real package and produce confusing import errors.
Rename the file, delete any generated __pycache__ folder, and run the program again.
Fix 7 — Repair package structure and imports
For a multi-folder project, add __init__.py files where appropriate and prefer running the package from its root. For example:
python -m app.main
This is often more reliable than running a deeply nested file directly because Python understands the package hierarchy.
Fix 8 — Inspect Python’s search path
python -c "import sys; print('
'.join(sys.path))"
The folder containing your module must appear in this list. Prefer correcting the project layout or launch directory rather than permanently editing PYTHONPATH. Environment-variable fixes can hide structural problems and later break on another machine.
When Pylance complains but the code still runs
If the program executes successfully but VS Code shows Import could not be resolved, the issue is with editor analysis rather than Python runtime. Select the same interpreter for Pylance, reload the window, and restart the language server.
Quick diagnosis checklist
- Does
sys.executablepoint to the expected environment? - Does
python -m pip show package-namereturn a location? - Is the import name correct?
- Are you running from the project root?
- Did you accidentally name a local file after a standard or third-party module?
FAQ
Why is the package listed by pip but still not found?
The pip command and the Python process are probably connected to different environments. Compare their executable paths.
Can reinstalling Python fix ModuleNotFoundError?
Sometimes, but it is rarely the first fix. Interpreter and environment mismatches are far more common.
What if only one file cannot import my module?
Check the folder from which that file is launched and use package-style execution from the project root.