Python environments in VS Code
An "environment" in Python is the context in which a Python program runs that consists of an interpreter and any number of installed packages.
Note: If you'd like to become more familiar with the Python programming language, review More Python resources.
Types of Python environments
Global environments
By default, any Python interpreter installed runs in its own global environment. For example, if you just run python, python3, or py at a new terminal (depending on how you installed Python), you're running in that interpreter's global environment. Any packages that you install or uninstall affect the global environment and all programs that you run within it.
Tip: In Python, it is best practice to create a workspace-specific environment, for example, by using a local environment.
Local environments
There are two types of environments that you can create for your workspace: virtual and conda. These environments allow you to install packages without affecting other environments, isolating your workspace's package installations.
Virtual environments
A virtual environment is a built-in way to create an environment. A virtual environment creates a folder that contains a copy (or symlink) to a specific interpreter. When you install packages into a virtual environment it will end up in this new folder, and thus isolated from other packages used by other workspaces.
Note: While it's possible to open a virtual environment folder as a workspace, doing so is not recommended and might cause issues with using the Python extension.
Conda environments
A conda environment is a Python environment that's managed using the conda package manager (see Getting started with conda).Choosing between conda and virtual environments depends on your packaging needs, team standards, etc.
Python environment tools
The following table lists the various tools involved with Python environments:
| Tool | Definition and Purpose |
|---|---|
| pip | The Python package manager that installs and updates packages. It's installed with Python 3.9+ by default (unless you are on a Debian-based OS; install python3-pip in that case). |
| venv | Allows you to manage separate package installations for different projects and is installed with Python 3 by default (unless you are on a Debian-based OS; install python3-venv in that case) |
| conda | Installed with Miniconda. It can be used to manage both packages and virtual environments. Generally used for data science projects. |
Creating environments
Using the Create Environment command
To create local environments in VS Code using virtual environments or Anaconda, you can follow these steps: open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)), search for the Python: Create Environment command, and select it.
The command presents a list of environment types: Venv or Conda.

If you are creating an environment using Venv, the command presents a list of interpreters that can be used as a base for the new virtual environment.

If you are creating an environment using Conda, the command presents a list of Python versions that can be used for your project.

After selecting the desired interpreter or Python version, a notification will show the progress of the environment creation and the environment folder will appear in your workspace.

Note: The command will also install necessary packages outlined in a requirements/dependencies file, such as
requirements.txt,pyproject.toml, orenvironment.yml, located in the project folder. It will also add a.gitignorefile to the virtual environment to help prevent you from accidentally committing the virtual environment to source control.
Create a virtual environment in the terminal
If you choose to create a virtual environment manually, use the following command (where ".venv" is the name of the environment folder):
# macOS/Linux
# You may need to run `sudo apt-get install python3-venv` first on Debian-based OSs
python3 -m venv .venv
# Windows
# You can also use `py -3 -m venv .venv`
python -m venv .venv
Note: To learn more about the
venvmodule, read Creation of virtual environments on Python.org.
When you create a new virtual environment, a prompt will be displayed in VS Code to allow you to select it for the workspace.

Tip: Make sure to update your source control settings to prevent accidentally committing your virtual environment (in for example
.gitignore). Since virtual environments are not portable, it typically does not make sense to commit them for others to use.
Create a conda environment in the terminal
The Python extension automatically detects existing conda environments. We recommend you install a Python interpreter into your conda environment, otherwise one will be installed for you after you select the environment. For example, the following command creates a conda environment named env-01 with a Python 3.9 interpreter and several libraries:
conda create -n env-01 python=3.9 scipy=0.15.0 numpy
Note: For more information on the conda command line, you can read Conda environments.
Additional notes:
-
If you create a new conda environment while VS Code is running, use the refresh icon on the top right of the Python: Select Interpreter window; otherwise you may not find the environment there.

-
To ensure the environment is properly set up from a shell perspective, use an Anaconda prompt and activate the desired environment. Then, you can launch VS Code by entering the
code .command. Once VS Code is open, you can select the interpreter either by using the Command Palette or by clicking on the status bar. -
Although the Python extension for VS Code doesn't currently have direct integration with conda
environment.ymlfiles, VS Code itself is a great YAML editor. -
Conda environments can't be automatically activated in the VS Code Integrated Terminal if the default shell is set to PowerShell. To change the shell, see Integrated terminal - Terminal profiles.
-
You can manually specify the path to the
condaexecutable to use for activation (version 4.4+). To do so, open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and run Preferences: Open User Settings. Then setpython.condaPath, which is in the Python extension section of User Settings, with the appropriate path.
Working with Python interpreters
Select and activate an environment
The Python extension tries to find and then select what it deems the best environment for the workspace. If you would prefer to select a specific environment, use the Python: Select Interpreter command from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)).

Note: If the Python extension doesn't find an interpreter, it issues a warning. On macOS 12.2 and older, the extension also issues a warning if you're using the OS-installed Python interpreter as it is known to have compatibility issues. In either case, you can disable these warnings by setting
python.disableInstallationChecktotruein your user settings.
The Python: Select Interpreter command displays a list of available global environments, conda environments, and virtual environments. (See the Where the extension looks for environments section for details, including the distinctions between these types of environments.) The following image, for example, shows several Anaconda and CPython installations along with a conda environment and a virtual environment (env) that's located within the workspace folder:

Note: On Windows, it can take a little time for VS Code to detect available conda environments. During that process, you may see "(cached)" before the path to an environment. The label indicates that VS Code is presently working with cached information for that environment.
If you have a folder or a workspace open in VS Code and you select an interpreter from the list, the Python extension will store that information internally. This ensures that the same interpreter will be used when you reopen the workspace.