This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
APython environment is a context in which you run Python code and includes global, virtual, and conda environments. An environment consists of an interpreter, a library (typically the Python Standard Library), and a set of installed packages. These components together determine valid language constructs and syntax, operating-system functionality that you can access, and packages you can use.
In Visual Studio on Windows, you use thePython Environments window, as described in this article, to manage environments and select one as the default for new projects. Other aspects of environments are found in the following articles:
For any given project, you canselect a specific environment rather than use the default.
For details on creating and using virtual environments for Python projects, seeUse virtual environments.
If you want to install packages in an environment, refer to thePackages tab reference.
To install another Python interpreter, seeInstall Python interpreters. In general, if you download and run an installer for a mainline Python distribution, Visual Studio detects that new installation and the environment appears in thePython Environments window and can be selected for projects.
Note
You can manage environments for Python code that open as a folder by selectingFile >Open >Folder. The Python toolbar allows you to switch between all detected environments, and also add a new environment. The environment information is stored in thePythonSettings.json
file in the Workspace.vs
folder.
If you're new to Python in Visual Studio, see the following articles for general background:
The environments that Visual Studio knows about are displayed in thePython Environments window. To open the window, use one of the following methods:
ThePython Environments window appears alongsideSolution Explorer in Visual Studio:
Visual Studio looks for installed global environments by using the registry (followingPEP 514), along with virtual environments and conda environments (seeTypes of environments). If you don't see an expected environment in the list, seeManually identify an existing environment.
When you select an environment in the list, Visual Studio displays various properties and commands for that environment on theOverview tab of thePython Environments window, such as the interpreter location. The commands at the bottom of theOverview tab each open a command prompt with the interpreter running. For more information, seePython Environments window tab reference - Overview.
Use the dropdown list under the list of environments to switch to different tabs such asPackages andIntelliSense. These tabs are also described in thePython Environments window tab reference.
Selecting an environment doesn't change its relation to any projects. The default environment, shown in boldface in the list, is the one that Visual Studio uses for any new projects. To use a different environment with new projects, use theMake this the default environment for new projects command. Within the context of a project, you can always select a specific environment. For more information, seeSelect an environment for a project.
To the right of each listed environment, is a control that opens anInteractive window for that environment. (In Visual Studio 2017 15.5 and earlier, another control appears that refreshes the IntelliSense database for that environment. For details about the database, seePython Environments window tab reference.)
Note
Although Visual Studio respects the system-site-packages option, it doesn't provide a way to change it from within Visual Studio.
If you don't see any environments in thePython Environments window, it means Visual Studio failed to detect any Python installations in standard locations. Maybe you installed Visual Studio 2017 or later but cleared all the interpreter options in the installer options for the Python workload. Similarly, you possibly installed Visual Studio 2015 or earlier but didn't install an interpreter manually. For more information, seeInstall Python interpreters.
If you know you have a Python interpreter on your computer but Visual Studio (any version) didn't detect it, use the+ Custom command to specify the interpreter location manually. For more information, see how tomanually identify an existing environment.
Visual Studio can work with global, virtual, and conda environments.
Each Python installation maintains its ownglobal environment. For example, Python 2.7, Python 3.6, Python 3.7, Anaconda 4.4.0, and so on. For more information, seeInstall Python interpreters.
Each environment is composed of the specific Python interpreter, its standard library, and a set of preinstalled packages. It also contains any other packages you install while the environment is activated. Installing a package into a global environment makes it available to all projects using that environment. If the environment is located in a protected area of the file system (withinc:\program files, for example), then installing packages requires administrator privileges.
Global environments are available to all projects on the computer. In Visual Studio, you select one global environment as the default, which is used for all projects unless you specifically choose a different one for a project. For more information, seeSelect an environment for a project.
Working in a global environment is an easy way to get started. Over time, environments can become cluttered with many different packages installed for different projects. The clutter can make it difficult to thoroughly test your application against a specific set of packages with known versions. But this kind of environment is what you would expect to set up on a build server or web server. Conflicts can also occur when two projects require incompatible packages or different versions of the same package.
For these reasons, developers often create avirtual environment for a project. A virtual environment is a subfolder in a project that contains a copy of a specific interpreter. If you activate the virtual environment, any packages you install are installed only in that environment's subfolder. When you run a Python program within the virtual environment, you can be confident that the program is running against only those specific packages.
Visual Studio provides direct support for creating a virtual environment for a project. If you open a project that contains arequirements.txt
file, Visual Studio prompts you automatically to create a virtual environment and install those dependencies. You see the same behavior when you create a project from a template that includesrequirements.txt
file.
At any time within an open project, you can create a new virtual environment. InSolution Explorer, expand the project node, right-clickPython Environments, and chooseAdd environment. InAdd Environment, chooseVirtual environment. For more information, seeCreate a virtual environment.
Visual Studio also provides a command to generate arequirements.txt
file from a virtual environment, making it easy to recreate the environment on other computers. For more information, seeUse virtual environments.
You can create a conda environment by using theconda
tool, or with integrated conda management in Visual Studio 2017 version 15.7 and later. A conda environment requires Anaconda or Miniconda. These platforms are available through the Visual Studio Installer. For more information, seeInstall Python support in Visual Studio.
In thePython Environments window (or from the Python toolbar), selectAdd Environment to open theAdd environment dialog.
In theAdd environment dialog, select theConda environment tab:
Configure the following fields:
Field | Description |
---|---|
Project | Identifies the project in which to create the environment. |
Name | Provides the name for the conda environment. |
Add packages from | Specifies how to add packages to the conda environment. -Environment file: Select this option if you have an environment.yml file that describes your dependencies. Enter the name of the file or browse (...) to the file location and select the file.-One or more Anaconda package names: Select this option if you want to list one or more Python packages or Python versions. The package list instructs conda to create a Python environment. To install the latest version of Python, use the python command. To install a specific version, use the commandpython=,major>.<minor> as inpython=3.7 . You can also use the package button to select Python versions and common packages from a series of menus. |
Set as current environment | Activates the new environment in the selected project after the environment is created. |
Set as default environment for new projects | Automatically sets and activates the conda environment in any new projects created in Visual Studio. This option is the same as using theMake this the default environment for new projects in thePython Environments window. |
View in Python Environments window | Specifies whether to show thePython Environments window after creating the environment. |
Important
When you create a conda environment, be sure to specify at least one Python version or Python package to ensure the environment contains a Python runtime. You can use anenvironments.yml
file or the package list. If you don't provide this specification, Visual Studio ignores the environment. The environment doesn't appear anywhere in thePython Environments window, it's not set as the current environment for a project, and it's not available as a global environment.
If you happen to create a conda environment without a Python version, use theconda info
command to see the locations of conda environment folders. You can then manually remove the subfolder for the environment from that location.
SelectCreate.
You can monitor creation of the conda environment in theOutput window. After creation completes, the output displays some command-line interface (CLI) instructions, such asactivate env
:
In Visual Studio, you can activate a conda environment for your project in the same way that you would any other environment. For more information, seeSelect an environment for a project.
To install more packages in the environment, use thePackages tab on thePython Environments window.
Note
For best results with conda environments, use conda 4.4.8 or later. Keep in mind that conda versions are different from Anaconda versions. You can install suitable versions of Miniconda (Visual Studio 2019 and Visual Studio 2022) and Anaconda (Visual Studio 2017) through the Visual Studio Installer.
To see the conda version, where conda environments are stored, and other information, run theconda info
command at an Anaconda command prompt (a command prompt where Anaconda is in the path):
conda info
Your conda environment folders appear as follows:
envs directories : C:\Users\user\.conda\envs c:\anaconda3\envs C:\Users\user\AppData\Local\conda\conda\envs
Because conda environments aren't stored with a project, they behave similarly to global environments. For example, installing a new package into a conda environment makes that package available to all projects that use the environment.
For Visual Studio 2017 version 15.6 and earlier, you can use conda environments by pointing to them manually as described inManually identify an existing environment.
Visual Studio 2017 version 15.7 and later detects conda environments automatically and displays them in thePython Environments window as described in the next section.
Use the following steps to identify an environment installed in a nonstandard location.
In thePython Environments window (or from the Python toolbar), selectAdd Environment to open theAdd environment dialog.
In theAdd environment dialog, on theExisting environment tab, set theEnvironment field to<Custom>:
After you select the<Custom> value, more fields are added to the dialog.
Set thePrefix path field to the path of the interpreter. You can set the field by browsing (...) to the path location.
After you select the path, the remaining fields are populated. Review the values and modify as needed. When you're ready, selectAdd.
You can also review and modify details of the environment at any time in thePython Environments window.
In thePython Environments window, select the environment, and then select theConfigure tab.
After you make changes, select theApply command.
You can also remove the environment by using theRemove command. For more information, seeConfigure tab. This command isn't available for autodetected environments.
If Visual Studio finds registry entries for an environment, but the path to the interpreter is invalid, then thePython Environments window shows the environment name in a strikeout font format as shown in the following image:
To correct an environment that you want to keep, first try using the environment installer'sRepair process. Most installers include a repair option.
If the Python environment doesn't have a repair option, or you want to remove an invalid environment, you can use the following steps to modify the registry directly. Visual Studio automatically updates thePython Environments window when you make changes to the registry.
Run theregedit.exe
executable to open the Registry editor.
Browse to the environment folder that corresponds to your configuration:
Python version | Folder |
---|---|
64-bit version | HKEY_LOCAL_MACHINE\SOFTWARE\Python orHKEY_CURRENT_USER\Software\Python |
32-bit version | HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Python |
IronPython | IronPython |
Expand the distribution and version node structure for your environment:
Distribution | Node |
---|---|
CPython | PythonCore ><Version node> |
Anaconda | ContinuumAnalytics ><Version node> |
IronPython | <Version node> |
Inspect the values under theInstallPath node:
Caution
Invalid settings in theHKEY_CURRENT_USER\SOFTWARE\Python key override the settings in theHKEY_LOCAL_MACHINE\SOFTWARE\Python key.
To remove a Python project, browse to the Python environment inSolution Explorer. Right-click the Python environment that you want to remove, and selectRemove.
If you want to keep the Python environment but remove it from your project, selectRemove. If you want to delete the environment permanently, selectDelete.
Was this page helpful?
Was this page helpful?