How to Install a Python Virtual Environment in VS Code

Learn how to set up a Python virtual environment in Visual Studio Code with step-by-step instructions, best practices, and troubleshooting tips. This guide helps homeowners, DIYers, and developers work cleanly on Python projects inside VS Code.

Install Manual
Install Manual Team
·5 min read
VS Code Virtual Env - Install Manual
Photo by 9sdworldvia Pixabay
Quick AnswerSteps

You can install a Python virtual environment in VS Code by creating a venv, activating it per your operating system, and selecting the venv as your interpreter in the VS Code Python extension. This isolates dependencies, prevents version conflicts, and supports reproducible development within VS Code.

What is a Python virtual environment and why use one in VS Code

A Python virtual environment (venv) is a self-contained directory that contains a Python interpreter and a focused set of packages for a single project. It keeps dependencies separate from other projects and from the system Python, avoiding conflicts when you switch tasks or teams. In VS Code, using a dedicated venv is especially valuable because the Python extension can automatically detect and switch interpreters, streamline auto-completion, linting, and debugging to align with the project’s dependencies. According to Install Manual, isolating dependencies with virtual environments reduces conflicts when working with VS Code. This approach not only helps you reproduce environments on different machines but also makes it easier to share a project with teammates without surprising them with dependency mismatches.

A well-managed venv acts like a clean slate for your project. It ensures that whatever you install or upgrade stays contained, so other projects aren’t affected. In the context of VS Code, this means your editor’s IntelliSense, formatter, and test runner all run against the same, known-good environment every time you open the workspace.

Prerequisites: Python, VS Code, and the Python extension

Before you create a virtual environment, verify you have the right foundation. Install Python 3.x from the official site and confirm by running python --version in a terminal. Install Visual Studio Code and the Python extension by Microsoft, which enables interpreter detection, IntelliSense, and debugging tailored for Python. If you are collaborating, include a requirements.txt file and a concise README describing how to recreate the venv. This groundwork makes the subsequent steps predictable and repeatable across machines and team members.

How VS Code helps manage environments for Python projects

VS Code serves as a lightweight hub for Python development and can automatically detect interpreters and switch between them based on the active workspace. The Python extension exposes commands to create and choose virtual environments, as well as to run tests inside the integrated terminal. With the environment bound to the project folder, you avoid global package conflicts and ensure consistent behavior. In practice you’ll see the current interpreter in the status bar; VS Code tunes linting, formatting, and autocompletion to match the environment’s installed packages. This tight integration makes VS Code a reliable choice for DIYers and homeowners seeking a reproducible setup.

Planning your environment and project structure for reliability

Effective environment planning includes naming conventions, project layout, and documentation. Place the virtual environment inside the project folder and name it .venv or venv to keep it out of the source tree. Document the Python version and the exact packages in a requirements.txt file, pin versions when stability is critical, and consider a small setup script to automate venv creation and package installation. This planning makes onboarding easier and helps reproduce environments on another machine or in CI.

Step 1: Create the virtual environment (venv)

Open your project folder in a terminal and run python -m venv .venv. This creates a self-contained environment with a dedicated interpreter and site-packages directory. Use a consistent folder name to avoid confusion across projects. If you prefer a different location, adjust the path accordingly and plan to update VS Code’s interpreter selection later. Creating the venv inside the project folder makes it portable for sharing and version control decisions.

Step 2: Activate the environment on Windows

On Windows, activate the venv by executing .venv\Scripts\activate.bat in Command Prompt or .venv\Scripts\Activate.ps1 in PowerShell. When activation succeeds, your prompt will prefix with the environment name, and Python will point to the venv’s site-packages. If you encounter execution policy issues in PowerShell, temporarily allow script execution with Set-ExecutionPolicy -ExecutionPolicy RemoteSigned. Always run the activation command from the project directory to ensure the correct venv is used for your session.

Step 3: Activate the environment on macOS/Linux

On macOS or Linux, activate with source .venv/bin/activate. The shell prompt should reflect the venv name, and python or pip will reference the local environment. If you forget to activate, VS Code might still run with the global interpreter, causing confusion. A quick check is to run which python (or python --version) after activation to confirm you’re using the correct Python binary.

Step 4: Select the interpreter in VS Code

In VS Code, press Ctrl/Cmd+Shift+P to open the Command Palette, then choose Python: Select Interpreter. Pick the interpreter inside your .venv folder, which will usually be named .venv/bin/python or .venv\Scripts\python.exe. If you don’t see the venv, reload VS Code or reopen the workspace. This step is essential because it ensures commands, debugging, and linting run against the project’s environment rather than the system Python.

Step 5: Install dependencies and verify the setup

With the venv activated and the interpreter selected, install your project’s dependencies using pip install -r requirements.txt. Verify the installation by listing installed packages (python -m pip list) and by running a quick script that imports each package. Maintain a requirements.txt to recreate the environment precisely, and consider freezing exact versions for stability. Finally, run your tests to confirm that the environment behaves as expected in VS Code.

Tools & Materials

  • Python 3.x distribution(Download from official Python site; ensure it’s added to PATH)
  • Visual Studio Code(Latest stable release)
  • Python extension for VS Code(Microsoft's Python extension for VS Code)
  • Terminal or Command Prompt(Built-in terminal in VS Code or system terminal)
  • Internet connection(To install packages and extensions)

Steps

Estimated time: 15-25 minutes

  1. 1

    Create the virtual environment

    Open a terminal in your project folder and run python -m venv .venv to create a self-contained environment. This isolates Python and installed packages from the system Python.

    Tip: Use a clear name like .venv to keep it obvious and portable.
  2. 2

    Activate the environment on Windows

    Run .venv\Scripts\activate.bat in Command Prompt or .venv\Scripts\Activate.ps1 in PowerShell. The prompt should show the venv name.

    Tip: If PowerShell blocks scripts, temporarily adjust the execution policy and retry.
  3. 3

    Activate the environment on macOS/Linux

    Run source .venv/bin/activate. The prompt should reflect the venv name and Python will point to the venv's interpreter.

    Tip: Use source to ensure shell functions load correctly for activation.
  4. 4

    Select the interpreter in VS Code

    Open the Command Palette and choose Python: Select Interpreter, then pick the .venv interpreter. This ensures VS Code uses the venv for running code, linting, and debugging.

    Tip: If you don’t see the venv, reload VS Code or reopen the workspace.
  5. 5

    Install and verify dependencies

    With the venv active, install dependencies via pip install -r requirements.txt. Verify with python -m pip list and run a quick import check to confirm packages load correctly.

    Tip: Keep a pinned requirements.txt for reproducible environments.
Pro Tip: Keep the virtual environment inside your project folder for portability.
Warning: Avoid using the global Python interpreter for project-specific work.
Note: Document your environment steps in a README to onboard collaborators.
Pro Tip: Automate setup with a small script to recreate the venv and install requirements.

Got Questions?

What is a Python virtual environment and why use one in VS Code?

A Python virtual environment creates an isolated space for a project's dependencies, preventing version conflicts. In VS Code, the Python extension can automatically use the interpreter from the active venv.

A virtual environment isolates project dependencies so they don't affect other projects. In VS Code, switch to the venv interpreter to keep dependencies separate.

How do I create a virtual environment from the terminal?

Open your project folder in a terminal and run python -m venv .venv (or venv) to create the environment. Then activate it depending on your OS.

Create the environment with python -m venv .venv, then activate it per your OS.

How do I activate the environment on Windows?

Run .venv\Scripts\activate.bat or .venv\Scripts\Activate.ps1 in PowerShell. The prompt should indicate the active environment.

Use the activate script in the Scripts folder to enable the venv on Windows.

How do I activate the environment on macOS/Linux?

Run source .venv/bin/activate in the terminal. Your prompt should show the venv name.

Source the activate script from the bin folder to enable the venv on macOS or Linux.

How do I configure VS Code to use the venv interpreter?

Open Command Palette, run Python: Select Interpreter, and choose the interpreter inside your .venv folder. Reload if needed.

Select the interpreter from the Command Palette so VS Code runs Python from your venv.

What if VS Code doesn't detect my venv?

Ensure the environment is activated in the terminal, then reload VS Code or reopen the workspace. Check that the venv is in your project.

Reload VS Code after creating or activating the venv; the interpreter should appear.

Watch Video

Main Points

  • Isolate dependencies with venv for each project
  • Always select the correct interpreter in VS Code
  • Document and version your environment setup
  • Test activation in VS Code's integrated terminal
Process infographic showing steps to set up Python virtual environment in VS Code
Setting up Python virtual environments in VS Code

Related Articles