How to activate venv in VSCode

To activate a venv in VSCode, select it with Python: Select Interpreter and open a new terminal, which runs the activation command for you, or run .venv\Scripts\Activate.ps1 on Windows or source .venv/bin/activate on macOS and Linux.

This guide covers creating the environment, activating it in every shell, checking that VS Code really uses it, and fixing the errors that stop activation.

How to activate venv in VSCode
In Python: Select Interpreter, pick the entry that points to .venv\Scripts\python.exe so new terminals activate that environment. (Image: code.visualstudio.com)

Activating venv in VSCode

Once VS Code knows which environment belongs to the workspace, every new integrated terminal activates it automatically. This is the route that needs no typed commands.

  1. Open your project folder with File > Open Folder.
  2. Press Ctrl+Shift+P (Cmd+Shift+P on a Mac) to open the Command Palette.
  3. Run Python: Select Interpreter and choose the entry for your .venv folder.
  4. Press Ctrl+Shift+` to open a new terminal.
  5. Check that the prompt now starts with (.venv), which shows the environment is active.

Terminals that were already open keep their old state, so always open a fresh one after you switch environments.

Importance of activation in VSCode

Activation decides which Python and which packages run when you type a command. VS Code tracks two separate things: the selected interpreter and the terminal's active environment.

What activation changes Effect in VS Code
PATH order The environment's Scripts (Windows) or bin (macOS, Linux) folder goes first, so python and pip run from the venv
Package installs python -m pip install puts packages in the venv, isolated from the global Python
Terminal prompt The prompt shows the environment name, such as (.venv)
VIRTUAL_ENV variable Set to the environment's path while it is active
Run, debug and IntelliSense Use the interpreter picked with Python: Select Interpreter, not the terminal's activation

Activation is not strictly required: Python's documentation notes you can run the environment's interpreter by its full path instead. Activation just saves typing that path.

Prerequisites for activating venv in VSCode

Requirement How to check or get it
Python 3 interpreter Run py -3 --version on Windows or python3 --version on macOS and Linux; you can install Python from the command line if it is missing
Visual Studio Code Any current release; get VS Code for Windows or Mac from the official installer
Python extension Open Extensions with Ctrl+Shift+X, search Python and install the Microsoft extension
A project folder Open it with File > Open Folder, or run code . inside it
macOS only VS Code does not support the system Python on macOS; Microsoft recommends a Homebrew install (brew install python3)

If you install Python while VS Code is open, close and reopen the terminal so it picks up the new PATH.

Step-by-step guide to creating a virtual environment in VSCode

This is the full sequence from an empty folder to a working, activated environment.

  1. Install Python and VSCode, plus the Python extension from the Extensions view.
  2. Open your project directory with File > Open Folder.
  3. Open the integrated terminal with Ctrl+`.
  4. Create the virtual environment with python -m venv .venv (Windows) or python3 -m venv .venv (macOS, Linux).
  5. Activate the virtual environment with .venv\Scripts\Activate.ps1 (PowerShell) or source .venv/bin/activate (bash, zsh).
  6. Confirm activation: the prompt starts with (.venv) and the Status Bar shows the venv's Python version.
  7. Use the virtual environment: install packages with python -m pip install <package> and run your script from the same terminal.

Create a virtual environment

VS Code can build the environment for you, which also selects it for the workspace in one pass.

  1. Press Ctrl+Shift+P and run Python: Create Environment.
  2. Select Venv as the environment type.
  3. Choose the Python interpreter the environment should be based on.
  4. Pick a requirements.txt or pyproject.toml if VS Code offers to install dependencies.
  5. Wait for the notification to finish; a .venv folder appears in the Explorer.
  6. Or open the Python sidebar, expand Environment Managers and select + for Quick Create, which uses the latest Python and names the folder .venv.

If uv is installed, the Python Environments extension uses it automatically for venv creation, controlled by the python-envs.alwaysUseUv setting.

Create a virtual environment in VS Code with Quick Create in Environment Managers
Select + next to Environment Managers for Quick Create; a Creating environment notification shows while the .venv folder is built. (Image: code.visualstudio.com)

Creating a virtual environment from the VS Code terminal

The command line route works in any shell and gives the same .venv folder.

python -m venv .venv

Runs Python's built-in venv module and creates a folder named .venv in the current directory with its own Python, pip, activation scripts and a pyvenv.cfg file. Use python3 instead of python on macOS and Linux.

You should see: A .venv folder containing Scripts (Windows) or bin (macOS, Linux) appears in the Explorer, and VS Code may ask whether to select it for the workspace.

Activate the virtual environment

Manual activation is a single command typed into the VS Code terminal from the project root.

  1. Open a terminal with Ctrl+` and make sure it is in the folder that contains .venv.
  2. Check which shell the terminal uses; its name appears in the terminal tab list on the right.
  3. Type the activation command for that shell from the table below and press Enter.
  4. Look for (.venv) at the start of the prompt.
  5. Run your Python commands in this same terminal; other open terminals are not affected.

Activating venv in different operating systems

The activation script lives in Scripts on Windows and bin on macOS and Linux, and each shell has its own script.

Operating system Shell Activation command
Windows PowerShell .venv\Scripts\Activate.ps1
Windows Command Prompt (cmd.exe) .venv\Scripts\activate.bat
macOS, Linux bash or zsh source .venv/bin/activate
macOS, Linux fish source .venv/bin/activate.fish
macOS, Linux csh or tcsh source .venv/bin/activate.csh
macOS, Linux PowerShell (pwsh) .venv/bin/Activate.ps1

How to activate venv in VS Code on Windows

On Windows, PowerShell and Command Prompt each have their own activation script in the Scripts folder.

  1. Press Ctrl+` to open the terminal in your project folder.
  2. Type .venv\Scripts\Activate.ps1 and press Enter.
  3. If PowerShell says the script cannot be loaded, open a Command Prompt terminal from the terminal dropdown instead and run .venv\Scripts\activate.bat.
  4. To keep using PowerShell, run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser, the setting Python's own documentation gives for Activate.ps1.
  5. Open a new terminal and run the activation command again.

Microsoft lists RemoteSigned as the default policy for Windows computers; it still blocks unsigned scripts downloaded from the internet, and the CurrentUser scope changes nothing for other accounts.

Activate venv in VS Code on Windows by choosing PowerShell or Command Prompt
If PowerShell blocks Activate.ps1, open a Command Prompt terminal from this dropdown and run .venv\Scripts\activate.bat instead. (Image: code.visualstudio.com)

How to activate venv on macOS and Linux

On macOS and Linux, use python3 to create the environment and source the script in bin.

  1. Open the terminal with Ctrl+` (the shortcut is the same on a Mac).
  2. Create the environment if needed with python3 -m venv .venv.
  3. Run source .venv/bin/activate in bash or zsh.
  4. Use source .venv/bin/activate.fish in fish, or source .venv/bin/activate.csh in csh.
  5. Install packages with python3 -m pip install <package> while (.venv) shows in the prompt.

Configure VSCode to use the virtual environment

Selecting the interpreter is what makes Run, Debug and IntelliSense use the venv, whatever the terminal shows.

  1. Open any .py file in the project.
  2. Select the Python version in the Status Bar at the bottom of the window.
  3. Pick the interpreter listed under your .venv folder.
  4. Or press Ctrl+Shift+P and run Python: Select Interpreter to choose from the same list.
  5. Open a new terminal so it activates the selected environment.

Without a manual choice, VS Code picks a workspace-local .venv or venv before any global interpreter. The debugger uses the selected environment unless launch.json sets a different python path.

Configuring VSCode for Python virtual environments

These settings come from the Python Environments extension and control how environments are found and activated.

Setting Default What it controls
python-envs.terminal.autoActivationType command How new terminals activate the venv: command runs it visibly, shellStartup activates through shell startup scripts, off disables it
python-envs.workspaceSearchPaths ./**/.venv Where VS Code looks for environments in the workspace; set it at workspace level
python-envs.globalSearchPaths empty Absolute paths for environments outside the workspace, such as a shared envs folder
python-envs.defaultEnvManager ms-python.python:venv Which manager Quick Create uses: venv or conda
python-envs.alwaysUseUv true Use uv for venv creation and installs when it is available
python.defaultInterpreterPath not set Legacy fallback interpreter, still honoured

Restart your terminals after you change autoActivationType. The old python.terminal.activateEnvironment setting maps to this one, and the new setting wins.

VS Code Python environments settings showing Workspace Search Paths for .venv
python-envs.workspaceSearchPaths defaults to ./**/.venv; add other folder names here at workspace level if your venv uses a different name. (Image: code.visualstudio.com)

Verifying the activation of venv

Three checks tell you whether the terminal and the editor both use the environment.

  1. Look at the terminal prompt: (.venv) at the start means the shell activated the environment.
  2. Look at the Status Bar: the Python version shown should be the one from .venv.
  3. Run the interpreter check below and confirm it prints True.
  4. Install or import a package to make sure it lands in the venv, not the global Python.

Check the Python interpreter

Python's documentation gives a reliable test: inside a virtual environment, sys.prefix differs from sys.base_prefix.

python -c "import sys; print(sys.prefix != sys.base_prefix, sys.prefix)"

sys.prefix points to the virtual environment while it is in use, and sys.base_prefix points to the Python installation it was created from.

You should see: The output starts with True followed by the path to your project's .venv folder. False means the terminal is still on the global Python.

Use the integrated terminal

The integrated terminal is where activation actually happens, and VS Code activates it for you once an environment is selected.

  1. Press Ctrl+Shift+` or select + in the Terminal panel to open a fresh terminal.
  2. Watch the first lines: with the default command activation type, VS Code types and runs the activation command visibly.
  3. To open a terminal for a different environment, right-click it in Environment Managers and select Open in Terminal.
  4. Close stale terminals with the trash can icon on their tab.
  5. Run scripts with Run Python File in Terminal from the editor's right-click menu so they use the selected interpreter.
VS Code integrated terminal running a script with the .venv Python interpreter
Run Python File in Terminal calls the python.exe inside .venv/Scripts, so the script runs with the environment's packages. (Image: code.visualstudio.com)

Test package availability

Installing and importing a package proves that pip and Python both point at the venv.

python -m pip install numpy

Uses the active interpreter's pip, so the package goes into .venv. Use python3 on macOS and Linux. Then run your script or python -c "import numpy" in the same terminal.

You should see: The import works without ModuleNotFoundError. If the error appears in Run or Debug but not in the terminal, the selected interpreter is not the venv.

You can also install packages without a terminal: right-click the environment in Environment Managers and select Manage Packages.

Integrating venv with VSCode features

Most VS Code features read the selected interpreter, so one choice covers the editor, the debugger and the tests.

VS Code feature Uses the venv when Note
Run Python File The venv is the selected interpreter Opens a terminal with the environment activated
Debugging The venv is selected, or set as python in launch.json launch.json overrides the selection
IntelliSense and Pylance The venv is selected Pylance uses one interpreter per workspace folder
Test Explorer Each Python project has an environment Each project gets its own test tree
Jupyter notebooks You pick the venv as the kernel Kernel list may differ from Environment Managers
Terminal autoActivationType is not off Only new terminals activate

Common issues and troubleshooting when activating venv in VSCode

The environment shows as (broken)

The pyvenv.cfg file is missing or the environment's Python executable is invalid.

  1. Delete the environment: right-click it in Environment Managers and select Delete.
  2. Create it again with Python: Create Environment.
  3. Reinstall packages with python -m pip install -r requirements.txt.

ModuleNotFoundError after installing a package

The package went into a different interpreter from the one running your code.

  1. Run Python: Select Interpreter and choose the .venv entry.
  2. Open a new terminal and run python -m pip install <package> again.
  3. Rerun the script from that terminal.

The venv stopped working after you moved the project folder

Scripts inside a venv store absolute paths, so environments are not portable.

  1. Save the package list with pip freeze > requirements.txt from the old environment if it still runs.
  2. Delete the old .venv folder.
  3. Create a new one in the new location and install from requirements.txt.

Issue: venv not showing up in Command Palette

Python: Select Interpreter does not list your venv

The folder is outside the search paths, or the discovery cache is stale.

  1. Press Ctrl+Shift+P and run Python Environments: Refresh All Environment Managers.
  2. If the venv is not named .venv, add its path to python-envs.workspaceSearchPaths in workspace settings.
  3. For an environment outside the project, add its absolute path to python-envs.globalSearchPaths.
  4. Run Python: Select Interpreter again.
Venv not showing in VS Code fixed with Refresh All Environment Managers
The refresh icon in Environment Managers runs Refresh All Environment Managers, which rescans the search paths for a missing venv. (Image: code.visualstudio.com)

Issue: Unable to activate venv via terminal

PowerShell refuses to run Activate.ps1

The execution policy for your account does not allow local scripts.

  1. Switch the terminal to Command Prompt and run .venv\Scripts\activate.bat.
  2. Or run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser once, as Python's documentation suggests.
  3. Check the result with Get-ExecutionPolicy -Scope CurrentUser, then open a new terminal.

The activate script is not found

The terminal is not in the project root, or the command uses the wrong folder for your OS.

  1. Run the command from the folder that contains .venv.
  2. Use Scripts on Windows and bin on macOS and Linux.
  3. Use source before the script name in bash, zsh, fish and csh.

Issue: VSCode not using the venv interpreter

The Status Bar or Run button still uses the global Python

Another interpreter is selected, or a setting such as python.defaultInterpreterPath overrides discovery.

  1. Select the Python version in the Status Bar and pick the .venv interpreter.
  2. Check .vscode/settings.json for python.defaultInterpreterPath or a python-envs.pythonProjects entry pointing elsewhere.
  3. Check launch.json for a python property if only debugging uses the wrong interpreter.
  4. Close old terminals and open a new one.
VS Code not using the venv interpreter: Global Python selected instead of .venv
Here the selected interpreter is the global Python 3.13; choose the .venv entry marked Workspace so Run, Debug and IntelliSense use it. (Image: code.visualstudio.com)

How to deactivate venv in VS Code

  1. Type deactivate in the terminal and press Enter; the (.venv) prefix disappears.
  2. To stop automatic activation, set python-envs.terminal.autoActivationType to off and restart your terminals.
  3. To remove the environment completely, right-click it in Environment Managers and select Delete.
  4. Select another interpreter afterwards, because projects that used the deleted environment need a new one.

Best practices for managing virtual environments in VSCode

Practice Why
One environment per project, named .venv VS Code finds ./**/.venv automatically and prefers it over global interpreters
Keep .venv out of Git Since Python 3.13, venv creates a .gitignore inside the environment by default
Keep a requirements.txt pip freeze > requirements.txt lets anyone rebuild the environment, and VS Code installs from it on create
Recreate instead of moving Environments contain absolute paths and break when moved
Commit .vscode/settings.json for teams Project settings store the environment manager, not machine-specific paths

Additional tips for VS Code virtual environments

Tip How
Load environment variables from a .env file Set python.terminal.useEnvFile to true; variables are injected when terminals open
Different venvs for different folders Right-click a folder in the Explorer and select Add as Python Project, then assign its environment
Faster installs Install uv; venv installs then use uv pip while python-envs.alwaysUseUv is on
Run a selection in the venv Select lines and press Shift+Enter to send them to the Python terminal
Open a Python REPL in the venv Run Python: Start Terminal REPL from the Command Palette

Activate venv in VS Code FAQ

How do I activate venv in the VS Code terminal?

Run .venv\Scripts\Activate.ps1 in a PowerShell terminal or .venv\Scripts\activate.bat in Command Prompt on Windows. On macOS and Linux, run source .venv/bin/activate. The prompt then starts with (.venv).

Does VS Code activate venv automatically?

Yes. After you pick the venv with Python: Select Interpreter, each new integrated terminal runs the activation command for you. Terminals opened earlier are not changed, so open a new one after switching.

How do I select a virtual environment in VS Code?

Press Ctrl+Shift+P, run Python: Select Interpreter and choose the interpreter inside your .venv folder. You can also select the Python version in the Status Bar to switch environments.

Why can't I activate venv in PowerShell?

PowerShell blocks Activate.ps1 when your execution policy does not allow local scripts. Use activate.bat in a Command Prompt terminal, or run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser as Python's documentation suggests.

How do I know if venv is activated?

The terminal prompt shows (.venv), and python -c "import sys; print(sys.prefix != sys.base_prefix)" prints True. The VIRTUAL_ENV variable is also set to the environment's path while it is active.

How do I deactivate a venv in VS Code?

Type deactivate in the terminal and press Enter. To stop VS Code activating it automatically, set python-envs.terminal.autoActivationType to off and restart your terminals.

Do I need to activate venv to run Python in VS Code?

No. Run and Debug use the selected interpreter whether or not a terminal is activated. Activation only changes which python and pip commands the terminal runs.

What is the difference between .venv and venv folders?

Only the name. Both are conventional names for a virtual environment folder, and VS Code checks workspace-local .venv and venv folders before global interpreters when it picks one automatically.

Final tips for using venv in VS Code

Create the environment with Python: Create Environment, name it .venv, and let VS Code activate it in new terminals instead of typing activation commands. That route selects the interpreter for Run, Debug and IntelliSense at the same time as the terminal, so the editor and the shell never disagree about which Python they use.

Philip Celasco

Philip is a Texas-based technology writer and IT administrator at Techdows.com with more than 10 years of experience creating practical content for everyday users and professionals. He specializes in web browsers, particularly Chromium-based platforms such as Google Chrome, Microsoft Edge, Brave, and Opera. Through his work as an IT administrator, Philip has hands-on experience managing devices, configuring browser policies, troubleshooting software and network issues, and helping people resolve problems that affect productivity and security. His articles are based on practical testing and real-world technical experience. He covers browser settings, extensions, performance problems, privacy controls, security features, and Windows troubleshooting. Outside work, Philip enjoys the quieter side of life in Texas and stepping away from the screen when he can. He has two kids, two cats and loves to play golf with his mother during the weekends.

Leave a Reply

Your email address will not be published. Required fields are marked *