Tools and workflows

How to select the Python interpreter in VS Code

Choose a workspace interpreter, verify it with sys.executable, and fix import errors caused by a terminal or debugger using a different Python.

Open your project folder in VS Code and choose Python: Select Interpreter from the Command Palette. Select the interpreter for that project, usually the executable inside its .venv folder. Save a file that prints sys.executable and run it through VS Code. Compare the output with a fresh terminal. Two environments can report the same Python version while having different packages.

Open the folder before selecting an interpreter

Open the folder with File → Open Folder. A workspace gives the Python extension a project boundary for discovering environments and storing a selection. Opening only a single file can make it harder to distinguish the settings for that project from a default environment.

Install both Python and the Python extension. The extension helps find an installed interpreter. If nothing appears in the picker, check that Python runs from the terminal first.

Compare the editor run with the terminal

Use a tiny script instead of inferring from an environment badge.

import sys
from pathlib import Path

print(sys.executable)
print(sys.version.split()[0])
print(Path.cwd())

Run the saved file using the Python extension's Run Python File in Terminal command. Then start a new terminal and execute the same file manually. Compare the executable and working directory in both outputs. Equal Python versions do not prove you used the same environment. Two different virtual environments may have been created from the same base interpreter.

If a terminal already existed before you selected an interpreter, its environment may still reflect an older selection. Try a fresh terminal. You can always invoke the exact executable path instead of depending on automatic activation. If your setup blocks activation scripts, follow your organisation's approved setup rather than changing global execution policy.

Diagnose a missing package in one place

If Run fails at import requests after you installed Requests in a terminal, use the executable reported by the Run diagnostic with -m pip show requests. Consult the project's dependencies before installing anything. See how to check where pip installed Requests if the editor and terminal disagree.

Also check the file name. A script named requests.py can shadow the installed library. A traceback pointing into the project folder is useful evidence. Rename your own file to describe its job and rerun the diagnostic from the same environment.

Check debug and test runs separately

A debug setup can specify its own interpreter or launch context. A test runner can discover tests from a different working directory than the one you used to run a file. If only one workflow fails, inspect that workflow instead of reinstalling Python all over again.

On a remote workspace, inspect the path printed inside the remote session. Windows Python and Linux Python in WSL use separate environments, even if they can both see your project files. Create the environment for the operating system where the code will execute.

Trace one concrete conflict

Imagine the integrated terminal prints C:\work\sample\.venv\Scripts\python.exe, while Run prints a different base interpreter. The versions can match exactly. Open Python: Select Interpreter while the project is active and choose the first executable. Run the diagnostic again before changing your requirements file or reinstalling a library. If you use a multi-root workspace, confirm which folder contains the active file and configure the relevant folder.

Now consider the reverse case. The Run button prints the intended path but an old terminal uses the base interpreter, because it was open before the environment selection changed. Opening a new terminal can pick up automatic activation where that feature is enabled. Calling the exact interpreter works even if auto activation is disabled or unavailable. Verify the output rather than assuming the setting acted immediately.

Separate environment discovery from installation

VS Code may not list an environment it cannot locate automatically. If you know the executable path and can run it from a terminal, look for the option to enter that path. If it cannot run outside VS Code either, recreate the environment from the project's supported Python. A virtual environment copied from another machine or moved with a renamed project may contain absolute references that no longer work.

Avoid putting real work into the environment directory itself. The dependency record, instructions, tests, and source are the parts you want to keep. When you reconstruct the interpreter environment, those files should remain available for comparison.

Distinguish a red underline from a runtime error

A Python language extension can display “import could not be resolved” without executing the program. The runtime may succeed because it uses another environment or because its launch command adjusts the search path. Conversely, a completion list can look fine while a test imports something different. Always check the exact tool that reported the issue. Compare actual interpreter paths and import locations before changing imports that already work.

Make the next machine easy to set up

Commit the Python version guidance and dependency instructions. Avoid sharing a machine-specific absolute path as if it were valid for everyone. The next reader can create a local environment, select it in their workspace, and verify it against the documented command. An exact path is a strong diagnostic for one machine and a poor shared project default.

Sources and further reading

Related reading

Browse Tools and workflows · Explore project tutorials

Search PythonProjects.org