How can I set up a virtual environment for Python in Visual Studio Code?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The clean way to use Python in Visual Studio Code is to create a project-local virtual environment and tell VS Code to use that interpreter. Once that is done, terminals, linting, debugging, and package installs all stay isolated to the project instead of leaking into your global Python setup.
Create the Virtual Environment in the Project Folder
Open the project folder in VS Code, then create a virtual environment from the integrated terminal.
On macOS or Linux:
On Windows:
Using .venv inside the project folder is common because VS Code detects it easily and the environment stays physically close to the code it belongs to.
Activate It in the Terminal
Activation is not what makes the environment real, but it is useful because it changes the shell session to use the virtual environment by default.
macOS or Linux:
Windows PowerShell:
After activation, install packages into the environment:
This keeps project dependencies isolated from system Python.
Select the Interpreter in VS Code
The crucial IDE step is choosing the interpreter that lives inside the virtual environment.
Use the command palette and select:
- '
Python: Select Interpreter'
Then pick the interpreter inside .venv. Once VS Code uses that interpreter, features such as linting, IntelliSense, testing, and debugging target the correct environment.
This step matters even if the terminal is already activated. The VS Code Python extension uses its own interpreter selection for editor features.
Confirm the Interpreter Really Changed
A quick check in the VS Code terminal is worth doing:
The printed path should point to .venv. If it still points to a global Python installation, the terminal or interpreter selection is not configured the way you think.
Install the Right Packages in the Right Place
Once the interpreter is selected, install project dependencies from the same environment.
If the project already has a requirements file:
That gives you a consistent project environment for both terminal commands and VS Code tooling.
Why This Is Better Than Global Installs
A virtual environment solves several real problems:
- Different projects can require different package versions.
- You avoid polluting the system interpreter.
- Project dependencies become easier to document.
- Rebuilding the environment is simpler when something breaks.
This is why Python projects generally treat a virtual environment as normal setup, not as an optional extra.
Common Pitfalls
- Creating the virtual environment but never selecting it in VS Code.
- Installing packages globally because the terminal was not activated.
- Opening the wrong folder in VS Code, which breaks interpreter auto-detection.
- Committing the
.venvdirectory to version control instead of ignoring it. - Assuming the editor and terminal always use the same interpreter automatically.
Summary
- Create a project-local environment with
python -m venv .venv. - Activate it in the terminal before installing packages.
- Use
Python: Select Interpreterin VS Code to point the editor at.venv. - Verify with
sys.executableso you know the right Python is active. - Keep
.venvout of version control and reinstall dependencies from a requirements file when needed.

