You are currently viewing VS Code ModuleNotFoundError After pip Install on Windows

VS Code ModuleNotFoundError After pip Install on Windows

  • Post author:
  • Post published:
  • Post category:Python

If VS Code reports ModuleNotFoundError after you ran pip install on Windows, start by checking which Python is running the failing file. Run a small probe with the same button or debugger configuration that produced the error. Then compare the executable it prints with the Python that owns your installed package. Two environments can have the same Python version and still contain completely different packages.

For a general terminal or command-line failure, start with our Windows guide to ModuleNotFoundError. Here we will isolate what changes inside VS Code, including the case where PowerShell works but F5 fails.

Print the executable from the action that fails

Create a file called check_runtime.py next to your program. Avoid naming it after a package you are diagnosing. Substitute the name used in your actual import statement for requests. For a dotted import such asrequests.auth, start with the first component.

import importlib.util
import sys

module_name = "requests"
spec = importlib.util.find_spec(module_name)
print("Executable:", sys.executable)
print("Python version:", sys.version.split()[0])
print("Module visible:", spec is not None)
if spec is not None:
    print("Module origin:", spec.origin)

Save the file. If the failure happened when you clicked the Python extension’s run button, use the Command Palette command Python: Run Python File in Terminal to run this file. If the failure is in a debug configuration that launches a specific application, add the probe statements temporarily above the failing import and press F5 with the same configuration. Leave any module docstring or from __future__ imports at the start of the entry file. A different debug configuration can produce a different answer. Remove the temporary probe after diagnosing the issue.

Now open the integrated terminal where you originally installed the package. In PowerShell, run these three commands. The first two compare the executable and pip that this particular terminal resolves. The last one checks whether that environment has the named distribution.

python -c "import sys; print(sys.executable)"
python -m pip --version
python -m pip show requests

Compare the full executable paths from the terminal and failing launch. Read the Location line from pip show as a separate clue. A result pointing to another project or a global Python installation explains why an earlier install could succeed without fixing this workspace. The installer and the failing launch both did what they were asked to do, but they were working in different places.

Choose the right fix for the path you see

Compare the Python executable reported by the terminal, Run button, and debugger.
Compare the Python executable reported by the terminal, Run button, and debugger.
  • Terminal and Run use different paths. Select your intended interpreter and open a fresh terminal. Confirm its path before installing anything.
  • Run works, but Debug uses a different path. Inspect the active launch configuration for a hard-coded python value.
  • The paths match, but pip cannot find the package. Install the project’s declared dependency with that interpreter.
  • The paths match, and pip lists the package, but the import still fails. Check the import name, the traceback’s final missing name, the file layout, and the actual import origin.
  • Only the editor underlines the import. Confirm the runtime result first. A Pylance diagnostic and a Python traceback are separate observations.

Use the project’s intended environment as your reference point. An existing repository may have an environment defined in its setup guide, a requirements file, or another dependency manager. If the interpreter that happens to work belongs to an unrelated project, switching everything to it may hide the missing setup for this one.

Make Run, and a fresh PowerShell terminal agree

Open the actual project folder in VS Code. Press Ctrl+Shift+P, choose Python: Select Interpreter, and select the environment you intend to use. Check the path in the picker or use Enter interpreter path if the environment is missing. For a project-local Windows virtual environment named .venv, the executable is normally .venv\Scripts\python.exe.

Use the terminal trash-can icon to close the affected shell once any work in it is saved, or open a new terminal and leave the old one alone. The important part is to compare a newly opened shell with the runner. An old PowerShell session may still have an earlier environment on its path. The prompt decoration can help, but the executable printed by Python is the more reliable signal. The VS Code environment documentation explains interpreter discovery and how terminal auto-activation works.

If your shell still points somewhere else, run the intended interpreter directly. From the project directory, use the following commands for a .venv environment. Change the path when the environment has a different location. This also works when PowerShell blocks activation scripts. The Python executable can run without calling Activate.ps1.

& .\.venv\Scripts\python.exe -m pip --version
& .\.venv\Scripts\python.exe -m pip show requests
& .\.venv\Scripts\python.exe -c "import requests; print(requests.__file__)"

If the project has a requirements.txt and its instructions tell you to use it, run & .\.venv\Scripts\python.exe -m pip install -r requirements.txt. For a standalone script where Requests is the actual dependency, use & .\.venv\Scripts\python.exe -m pip install requests. The pip user guide explains invoking pip as a module. This form binds pip to the executable at the start of the command.

When there is no project environment, use the Windows walkthrough for creating a Python virtual environment, then repeat interpreter selection and installation. Working inside the project environment keeps the repair scoped to this workspace. A global install or a change to Windows execution policy is unnecessary for the direct-executable commands above.

Fix F5 when the terminal and Run already work

Open Run and Debug and check the configuration name shown in its dropdown. In a workspace with a .vscode\launch.json file, find the matching name entry. A configuration can specify a python executable independently of the interpreter selected for the editor. That is useful for teams that deliberately debug two environments, but an old absolute path can survive a project move or a renamed virtual environment.

If the configuration should follow your selected interpreter, remove the obsolete python property. Preserve the other fields, including program or module, environment variables and arguments. If the different interpreter is intentional, update only that value to the intended executable and install dependencies there. The Microsoft Python debugger documentation describes the default and the override.

Run the same debug configuration again with the probe still in place. A successful terminal import cannot prove that F5 uses the same interpreter. The repair is verified when the debugger prints the desired path and the exact import succeeds. If both paths already matched, inspect the debug configuration’s working directory and launched module. Those settings can change which local modules Python is able to find.

What a tested mismatch looks like

The empty Python environment raises ModuleNotFoundError and the installed environment imports the demo package.
The empty Python environment raises ModuleNotFoundError, and the installed environment imports the demo package.

A Windows test for this guide used two virtual environments built from the same Python 3.14.10 installation. Both had pip 26.2.2. The test installed a small offline package into one environment,.installed, and left the second,.empty, alone. Here are the observed import outcomes, with machine-specific paths omitted.

.empty\Scripts\python.exe
ModuleNotFoundError: No module named 'cybo_demo'

.installed\Scripts\python.exe
local test package

The first command exited with code 1. The second exited with code 0. This was a PowerShell test of the interpreter behavior. The VS Code Run and Debug comparisons above tell you which executable is playing each role on your computer. You can get the same diagnostic signal without creating extra environments. Print the active executable in the failing context and compare it with pip’s location in the working context.

Check these cases when the interpreter paths match

The package label differs from the import name

Check the project’s dependency list and the package’s documentation. The name you give pip can differ from the name after import. For example, a project that depends on Pillow imports from PIL. Installing something with a name copied straight out of a traceback can add the wrong package. Use the known distribution name with pip show and the documented import name in the probe.

The traceback names a dependency farther down

Read the last missing name and the frames immediately above it. Your code may have found the package you asked for, while that package failed to import another module. Confirm whether the missing module comes from a declared project dependency or an optional feature you enabled. Install your project’s pinned dependencies or the documented extra into the same environment, then rerun the failing action.

A local file takes precedence over the installed module

If the probe points to a requests.py file in your project, you are seeing that local file rather than the expected package directory. Use a distinct filename for your own module and update any references to it. If you recently renamed a file or changed environments, start a fresh Python process before checking again. A running interpreter can keep imported module objects in memory.

The import is local to your repository

If a missing name belongs to your own code under a directory such as src, check the repository’s setup instructions and launch method. The package may need to be installed into the environment in editable mode, or the program may be intended to run as a module from the project root. Check those instructions and your debug configuration before adding permanent path overrides. Also confirm that the workspace folder and working directory point to the project you meant to open.

Verify the repair with the original action

Run the real program with the button or F5 configuration that produced the error. Check that the expected executable still appears and that the failing import now succeeds. If you also use the integrated terminal for installs, verify it python -m pip --version once more. Then remove the probe. Keeping the environment choice in your project setup notes gives the next developer an exact way to reproduce the fix.