Switch between Python environments - Visual Studio (Windows) (original) (raw)

All code in a Python project runs within the context of a specific environment. These environments can be a global Python environment, an Anaconda environment, a virtual environment, or a conda environment. Visual Studio uses the Python environment for debugging, import and member completions, and syntax checking. The environment is used for any tasks that require language services that are specific to the Python version and a set of installed packages.

In Visual Studio, you can create multiple environments for a project and switch between them according to your specific development needs. All new Python projects are initially configured to use the default global environment. You can see the environments for your project under the Python Environments node in Solution Explorer:

Screenshot that shows the global default Python environment for a project in Solution Explorer in Visual Studio.

Prerequisites

Switch the current project environment

In Visual Studio, you can change the active (current) environment for a Python project in Solution Explorer or from the toolbar by using the Add Environment feature.

  1. Start the Add Environment process:
    • In Solution Explorer, right-click the Python Environments node for your project and select Add Environment.
    • Or, on the Python toolbar, select Add Environment from the Environment dropdown menu.
      Screenshot that shows the two options to access the Add Environments feature in Visual Studio.
  2. In the Add Environment dialog, select the Existing environment tab. Expand the Environment dropdown list and choose your desired environment, then select Add.
    Screenshot that shows how to select a project environment in the Add Environments dialog in Visual Studio.

Use virtual environments

A virtual environment is a unique combination of a specific Python interpreter and a specific set of libraries that's different from other global and conda environments. A virtual environment is specific to a project and is maintained in a project subfolder. The folder contains the environment's installed libraries along with a pyvenv.cfg file that specifies the path to the environment's base interpreter on the file system. (A virtual environment doesn't contain a copy of the interpreter, only a link to it.)

One benefit to using a virtual environment is that as you develop your project over time, the virtual environment always reflects the exact dependencies of your project. This behavior is different from a shared global environment, which contains any number of libraries whether you use them in your project or not. From a virtual environment, you can easily create a requirements.txt file, which is used to reinstall package dependencies on other development or production computers. For more information, see Manage required packages with requirements.txt.

When you open a project in Visual Studio that contains a requirements.txt file, Visual Studio automatically gives you the option to recreate the virtual environment. On computers where Visual Studio isn't installed, you can use the pip install -r requirements.txt command to restore the necessary packages.

Because a virtual environment contains a hard-coded path to the base Python interpreter, and you can recreate the environment by using the requirements.txt file, you typically omit the environment subfolder from source control. After you add a virtual environment to your project, it appears in the Python Environments window. You can then activate it like any other environment and manage its packages.

Create a virtual environment

You can create a new virtual environment directly in Visual Studio as follows:

  1. Start the Add Environment process:
    • In Solution Explorer, right-click the Python Environments node for your project and select Add Environment.
    • Or, on the Python toolbar, select Add Environment from the Environment dropdown menu.
  2. In the Add Environment dialog, select the Virtual environment tab:
    Screenshot of the Virtual environment tab of the Add Environment dialog box in Visual Studio.
  3. Configure the required fields:
    Required field Description
    Project Identify the project in which to create the environment.
    Name Provide the name for the new virtual environment.
    Base interpreter Specify the base language interpreter for the virtual environment.
    Location The system assigns the default location for the virtual environment. To change the location, select the Change virtual environment location link, browse to the location and choose Select folder.
  4. Configure any desired optional fields:
    Optional field Description
    Install packages from file Specify the path to a requirements.txt file to add packages to the virtual environment. Enter the location and name of the file or browse (...) to the location and select the file.
    Set as current environment Activate the new environment in the selected project after the environment is created.
    Set as default environment for new projects Automatically set and activate the environment in any new projects created in Visual Studio. This setting is also available via the Make this the default environment for new projects option in the Python Environments window. When you use this option, place the virtual environment in a location outside of a specific project.
    View in Python Environments window Specify whether to show the Python Environments window after you create the new environment.
    Make this environment available globally Specify whether the virtual environment should also act as a global environment. When you use this option, place the virtual environment in a location outside of a specific project.
  5. Select Create to finalize the virtual environment.

Visual Studio displays a progress bar while it configures the environment and downloads any necessary packages.

After the process completes, Visual Studio activates the new virtual environment and adds it to the Python Environments node in Solution Explorer. The environment is also available in the Python Environments window for the containing project.

Activate an environment

Follow these steps to activate an existing environment for a project:

  1. In Solution Explorer, expand the Python Environments node for your project, and locate the environment you want to use.
  2. Right-click the environment, and select Activate Environment.
    Screenshot that shows how to activate a project environment in Visual Studio.
    If Visual Studio detects a requirements.txt file in that environment, it asks whether to install those packages.
    After Visual Studio activates the environment, the name of the active environment is shown in a bold font in Solution Explorer:
    Screenshot that shows how Visual Studio shows the name of the active environment in a bold font in Solution Explorer.

Remove a virtual environment

Follow these steps to remove an existing environment for a project:

  1. In Solution Explorer, right-click the virtual environment and select Remove.
  2. Visual Studio asks whether you want to remove or delete the virtual environment.
    • Select Remove to make the environment unavailable to the project but leave it on the file system.
    • Select Delete to both remove the environment from the project and delete it from the file system. The base interpreter is unaffected.

View and manage installed packages

In Solution Explorer, you can view and manage the packages installed in an environment. These packages can be imported and used in your code when the environment is active.

Considerations about package installation

When you work with packages in Visual Studio, keep in mind the following considerations: