askvity

How to activate virtual environment in Visual Studio Code terminal?

Published in VS Code Python Environment 2 mins read

To activate a virtual environment in the Visual Studio Code (VS Code) terminal, you primarily need to select the correct Python interpreter for your project within VS Code itself. Once selected, new terminals opened within VS Code will automatically use and activate that environment.

Steps to Activate Your Environment

Activating your virtual environment in the VS Code terminal is a straightforward process centered around selecting the desired Python interpreter. Follow these steps based on the provided reference:

  1. Open the Command Palette: Press Ctrl + Shift + P (or Cmd + Shift + P on macOS). This opens the VS Code Command Palette, a powerful tool for accessing various commands.
  2. Select the Interpreter Command: Type "Python: Select Interpreter" into the command palette search bar and click on the command when it appears.
  3. Choose Your Virtual Environment: A list of available Python interpreters and virtual environments will appear. Select the virtual environment you wish to use for your project.
  • Insight: Selecting the interpreter tells VS Code which Python installation and associated packages to use when running scripts, debugging, and opening new integrated terminals.
  • Benefit: As stated in the reference, your chosen virtual environment will be remembered, for all code in project. This means VS Code will automatically use this environment for your project going forward.

Once you have completed these steps, open a new terminal within VS Code (Terminal > New Terminal). The new terminal should automatically activate the virtual environment you selected. You will typically see the environment's name (e.g., (venv), (.venv)) prefixing your terminal prompt, indicating it is active.

Related Articles