Review Python interpreters
The following table lists Python interpreters that can be used with Visual Studio.
Interpreter | Description | Notes |
CPython | The “native” and most commonly used interpreter, available in 32-bit and 64-bit versions (32-bit recommended). Includes the latest language features, maximum Python package compatibility, full debugging support, and interop with IPython. Review the considerations in Should I use Python 2 or Python 3? to help determine which version of Python to install. | Visual Studio 2015 and earlier don’t support Python 3.6 or later, and can return errors like Unsupported python version 3.6. For Visual Studio 2015 and earlier, use Python 3.5 or earlier. |
IronPython | A .NET implementation of Python, available in 32-bit and 64-bit versions. Provides C#/F#/Visual Basic interop, access to .NET APIs, standard Python debugging (but not C++ mixed-mode debugging), and mixed IronPython/C# debugging. | IronPython doesn’t support virtual environments. |
Anaconda | An open data science platform powered by Python. Includes the latest version of CPython and most of the difficult-to-install packages. | If you’re unable to decide on an interpreter, we recommend using Anaconda. |
PyPy | A high-performance tracing JIT implementation of Python. Good for long-running programs and situations where you identify performance issues but can’t find other resolutions. | Works with Visual Studio but with limited support for advanced debugging features. |
Jython | An implementation of Python on the Java Virtual Machine (JVM). Similar to IronPython, code running in Jython can interact with Java classes and libraries. However, many of the libraries intended for CPython might not be accessible. | Works with Visual Studio but with limited support for advanced debugging features. |
Test your install
Quickly check your installation of Python support:
-
Launch Visual Studio.
-
Select Alt + I to open the Python Interactive window.
-
In the window, enter the statement
2+2
.The statement output
displays in the window. If you don’t see the correct output, recheck your steps.
Feedback
Submit and view feedback for
After installing Visual Studio 2019, I have problems with my anaconda. When I create a new environemnt in my anaconda, after hours I face the problem when I try to activate the environment and after that when I open any jupyter notebook, the kernel will not work. I copied the messages when I activate environment. How can I recover my anaconda environments? I have uninstalled the Visual Studio and deleted all its files but the problem is still here. I appreciate any help to solve this issue. I am using Windows 10 and Anaconda 3.
(base) C:\>conda activate enviro C:\>SET DISTUTILS_USE_SDK=1 C:\>SET MSSdk=1 C:\>SET "VS_VERSION=15.0" C:\>SET "VS_MAJOR=15" C:\>SET "VS_YEAR=2017" C:\>set "MSYS2_ARG_CONV_EXCL=/AI;/AL;/OUT;/out" C:\>set "MSYS2_ENV_CONV_EXCL=CL" C:\>set "PY_VCRUNTIME_REDIST=\bin\vcruntime140.dll" C:\>set "CXX=cl.exe" C:\>set "CC=cl.exe" C:\>set "VSINSTALLDIR=" C:\>for /F "usebackq tokens=*" %i in (`vswhere.exe -nologo -products * -version [15.0,16.0) -property installationPath`) do (set "VSINSTALLDIR=%i\" ) C:\>if not exist "" (for /F "usebackq tokens=*" %i in (`vswhere.exe -nologo -products * -requires Microsoft.VisualStudio.Component.VC.v141.x86.x64 -property installationPath`) do (set "VSINSTALLDIR=%i\" ) ) C:\>if not exist "" (set "VSINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\" ) C:\>if not exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\" (set "VSINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\" ) C:\>if not exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\" (set "VSINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\" ) C:\>if not exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\" (set "VSINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\" ) C:\>IF NOT "" == "" ( set "INCLUDE=;" set "LIB=;" set "CMAKE_PREFIX_PATH=;" ) C:\>call :GetWin10SdkDir C:\>call :GetWin10SdkDirHelper HKLM\SOFTWARE\Wow6432Node 1>nul 2>&1 C:\>if errorlevel 1 call :GetWin10SdkDirHelper HKCU\SOFTWARE\Wow6432Node 1>nul 2>&1 C:\>if errorlevel 1 call :GetWin10SdkDirHelper HKLM\SOFTWARE 1>nul 2>&1 C:\>if errorlevel 1 call :GetWin10SdkDirHelper HKCU\SOFTWARE 1>nul 2>&1 C:\>if errorlevel 1 exit /B 1 C:\>exit /B 0 C:\>for /F %i in ('dir /ON /B "\include\10.*"') DO (SET WindowsSDKVer=%~i ) The system cannot find the file specified. C:\>if errorlevel 1 (echo "Didn't find any windows 10 SDK. I'm not sure if things will work, but let's try..." ) else (echo Windows SDK version found as: "" ) Windows SDK version found as: "" C:\>IF "win-64" == "win-64" ( set "CMAKE_GEN=Visual Studio 15 2017 Win64" set "BITS=64" ) else ( set "CMAKE_GEN=Visual Studio 15 2017" set "BITS=32" ) C:\>pushd C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\ The system cannot find the path specified. C:\>CALL "VC\Auxiliary\Build\vcvars64.bat" -vcvars_ver=14.16 The system cannot find the path specified. C:\>popd C:\>IF "" == "" SET "CMAKE_GENERATOR=Visual Studio 15 2017 Win64" C:\>call :GetWin10SdkDirHelper HKLM\SOFTWARE\Wow6432Node 1>nul 2>&1 C:\>if errorlevel 1 call :GetWin10SdkDirHelper HKCU\SOFTWARE\Wow6432Node 1>nul 2>&1 C:\>if errorlevel 1 call :GetWin10SdkDirHelper HKLM\SOFTWARE 1>nul 2>&1 C:\>if errorlevel 1 call :GetWin10SdkDirHelper HKCU\SOFTWARE 1>nul 2>&1 C:\>if errorlevel 1 exit /B 1 C:\>exit /B 0 (enviro) C:\>
Setup Anaconda (Python) to Work With Visual Studio Code on Windows
Visual Studio Code and Anaconda are powerful tools for Python developers. However, if you are trying to use Anaconda and Visual Studio Code together there is a good chance you have run into some problems. As common as these tools are for programming with Python, it can be difficult to figure out how to get them to work together.
In Visual Studio Code you can run Python code with Anaconda by using the Anaconda Prompt, updating the Visual Studio Code workspace settings to be aware of your Anaconda installation, or adding Anaconda to the Windows Path variable.
This article will describe how to implement all three of these methods with step-by-step instructions. I have a FREE full course on installing Python and Anaconda for VSC. You can also check out this linked article for more ways to run Python code in VSC.
Use the Anaconda Prompt
My preferred way to use Anaconda with Visual Studio Code is to write the code in VSC and run the code from the Anaconda Prompt. To do this, simply open the Anaconda Prompt. You can find the Anaconda Prompt by searching in the Windows menu. This will open a command line terminal like the Windows command prompt, except this terminal is configured to use all the Anaconda tools.
Once the Anaconda Prompt is open, type “conda” and press enter. You should get a help message that explains usage for
conda
. If you get a message like the following, you do not have the correct prompt open.
'conda' is not recognized as an internal or external command, operable program or batch file.
Once you’ve confirmed that you have access to the Anaconda tools from the Anaconda Command Prompt, you can run the Python scripts you develop in VSC from the Anaconda Prompt. If you’re not sure how to do this, check out this tutorial for a step-by-step guide.
This is how I use Anaconda and Python with VSC almost all of the time. VSC gives me a great code editor and using the Anaconda Prompt to run the code helps me keep my environment settings clean.
Phản hồi
Gửi và xem ý kiến phản hồi dành cho
-
win-64
v14.0
conda install
To install this package run one of the following:
conda install ukoethe::visual-studio
To install this package run one of the following:
-
win-64
v14.0
conda install
To install this package run one of the following:
conda install ukoethe::visual-studio
To install this package run one of the following:
Install Python support in Visual Studio
Python support is available only on Visual Studio for Windows. On Mac and Linux, Python support is available through Visual Studio Code.
Download and install the Python workload
Complete the following steps to download and install the Python workload.
-
Download and run the latest Visual Studio Installer for Windows. Python support is present in release 15.2 and later. If you have Visual Studio installed already, open Visual Studio and run the installer by selecting Tools > Get Tools and Features.
Tip
The Community edition is for individual developers, classroom learning, academic research, and open source development. For other uses, install Visual Studio Professional or Visual Studio Enterprise.
-
The installer provides a list of workloads that are groups of related options for specific development areas. For Python, select the Python development workload and select Install:
Python installation options Description Python distributions Choose any combination of Python distribution that you plan to work with. Common options include 32-bit and 64-bit variants of Python 2, Python 3, Miniconda, Anaconda 2, and Anaconda 3. Each option includes the distribution’s interpreter, runtime, and libraries. Anaconda, specifically, is an open data science platform that includes a wide range of preinstalled packages. Visual Studio automatically detects existing Python installations. For more information, see The Python Environments window. Also, if a newer version of Python is available than the version shown in the installer, you can install the new version separately and Visual Studio detects it. Cookiecutter template support Install the Cookiecutter graphical UI to discover templates, input template options, and create projects and files. For more information, see Use the Cookiecutter extension. Python web support Install tools for web development including HTML, CSS, and JavaScript editing support, along with templates for projects using the Bottle, Flask, and Django frameworks. For more information, see Python web project templates. Python native development tools Install the C++ compiler and other necessary components to develop native extensions for Python. For more information, see Create a C++ extension for Python. Also install the Desktop development with C++ workload for full C++ support. By default, the Python workload installs for all users on a computer under:
%ProgramFiles%\Microsoft Visual Studio\
\
Common7\IDE\Extensions\Microsoft\Python
where
is 2022 and
is Community, Professional, or Enterprise.
%ProgramFiles(x86)%\Microsoft Visual Studio\
\
Common7\IDE\Extensions\Microsoft\Python
where
is 2019 or 2017 and
is Community, Professional, or Enterprise.
Fix or delete invalid environments
If Visual Studio finds registry entries for an environment, but the path to the interpreter is invalid, then the Python 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’s Repair process. Most installers include a repair option.
visual studio professional 2019 python/conda environment not retrieved
Despite having anaconda installed on my system I am not able to add a coda environment.
Visual Studio does not seem to retrieve it since the menu where I should be able to add conda environment asks me to install either anaconda or minoconda.
From the help it mentions that I should be able to install both via the visual studio installer but in my case only miniconda is proposed and I would like to use the anaconda installed on my system.
Anyone have an idea how to tell Visual Studio where to look at ? Regards
-
2022-01-17T05:29:37.967+00:00
Hello @oserin , welcome to Microsoft Q&A forum.
Did you choose to register Anaconda as the default Python environment during the installation of Anaconda? Have you checked the Python Environments window in Visual Studio 2019?
If possible, I suggest you try to reinstall Anaconda and keep the installing directory as default, relaunch VS and check again.
Feel free to contact me.
Best Regards,
Tianyu
-
2022-01-27T08:50:01.817+00:00
Hi @Tianyu Sun-MSFT , thank you for the hint concerning the registering option during install.
I was not aware since IT service takes care of this at my offcie, just checking with anaconda support if they know about a possibility to do after installation.I will make you know.Kind Regards
-
2022-02-15T09:07:10.097+00:00
Hi @oserin , I have not heard from you for a couple of days, may I ask if this issue has been solved? Feel free to let me know if there is anything that I can help here.
Kind Regards,
Tianyu
Sign in to comment
Install Python interpreters
There are several options for installing Python interpreters to work with Visual Studio. You can install an interpreter when you install the Python workload, or you can install an interpreter after a workload is present. Interpreters can also be installed manually outside of the Visual Studio Installer.
When you install the Python development workload in Visual Studio 2017 and later, Python 3 (64-bit) also installs by default. As an option, you can choose to install the 32-bit or 64-bit version of Python 2 or Python 3, along with Miniconda (Visual Studio 2019) or Anaconda 2/Anaconda 3 (Visual Studio 2017). The steps for this type of installation are described in Install Python support in Visual Studio.
An alternate approach is to install standard Python interpreters by using the Add Environment feature in Visual Studio. This option is available in the Python Environments window and the Python toolbar.
Python interpreters can also be installed manually outside of the Visual Studio Installer. Suppose you install Anaconda 3 before you install Visual Studio. You don’t need to reinstall Anaconda through the Visual Studio Installer. You can also install a newer version of an interpreter if it isn’t yet listed in the Visual Studio Installer.
Conclusion
Visual Studio Code is my go-to IDE/editor for Python development and Anaconda is my preferred Python distribution and environment management system. Over the years I’ve found it easiest to use VSC to write and edit code and the Anaconda Prompt to run Python code. If you’re looking for a more integrated approach, updating the settings JSON for each project is flexible and lets you work completely in VSC. For those that know what they’re doing and have a good reason, you can even add Anaconda to your system environment variables. Whichever option you choose integrating VSC and Anaconda gives you a powerful Python development environment.
Whether you’re looking to take your GIS skills to the next level, or just getting started with GIS, we have a course for you! We’re constantly creating and curating more courses to help you improve your geospatial skills.
All of our courses are taught by industry professionals and include step-by-step video instruction so you don’t get lost in YouTube videos and blog posts, downloadable data so you can reproduce everything the instructor does, and code you can copy so you can avoid repetitive typing
Add Anaconda Tools to the Windows PATH Variable
In VSC you’ll notice that if you type
conda
in the command prompt you will get the error described above. And if you type
python
in the command prompt it may take you to the Windows store to install Python. This is because the Windows command prompt does not know that you have Anaconda and its Python distribution installed.
To fix this add the path to the Anaconda python.exe and Scripts folder to the Windows PATH variable. As I mentioned above, you have the option to do this when Anaconda is installed. I do not recommend this method, and it’s not recommended by Anaconda, but it may be what some users want to do.
To add to the Windows PATH variable search for ‘Environment Variables’ from the Windows Start Menu and select the option to “Edit the system environment variables”.
In the window that opens select “Environment Variables”
Now you have the option to change environment variables for the current user or the system. You can add the appropriate file paths to either the Path (or PATH) variable for either the User or the System.
Select the “Path” variable and click “Edit”. This will open a new window with a list of file paths. Select “New” to add a new path. Add the following two paths to the “Path” variable.
- C:\Users\your-user-name\Anaconda3\
- C:\Users\your-user-name\Anaconda3\Scripts
Now click OK to close all the open Environment Variable windows.
Now when you type
conda
into the command prompt a usage information message should display.
Modify the registry to correct an environment
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 the regedit.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 or HKEY_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 > Anaconda ContinuumAnalytics > IronPython -
Inspect the values under the InstallPath node:
- If the environment still exists on your computer, change the value of the ExecutablePath entry to the correct location. Also correct the values for the (Default) and WindowedExecutablePath entries, as necessary.
- If the environment no longer exists on your computer and you want to remove it from the Python Environments window, delete the version number parent node of the InstallPath node. You can see an example of this node in the preceding image. In the example, this node is 3.6.
Caution
Invalid settings in the HKEY_CURRENT_USER\SOFTWARE\Python key override the settings in the HKEY_LOCAL_MACHINE\SOFTWARE\Python key.
Prerequisites
-
Visual Studio supports Python version 3.7. While it’s possible to use an earlier version of Visual Studio to edit code written in earlier versions of Python, those versions of Python aren’t officially supported. Visual Studio features such as IntelliSense and debugging might not work with earlier versions of Python.
-
For Visual Studio 2015 and earlier, use Python 3.5 or earlier. You must manually install one of the Python interpreters.
Anaconda distributions
Although Visual Studio offers to install the Anaconda distribution, your use of the distribution and other packages from Anaconda Repository are bound by the Anaconda Terms of Service. These terms might require some organizations to pay Anaconda for a commercial license, or else configure the tools to access an alternate repository. For more information, see the Conda channels documentation.
Move an interpreter
If you move an existing interpreter to a new location by using the file system, Visual Studio doesn’t automatically detect the change.
-
If you originally specified the location of the interpreter through the Python Environments window, you can edit its environment by using the Configure tab in that window to identify the new location. For more information, see Manually identify an existing environment.
-
If you installed the interpreter by using an installer program, use the following steps to reinstall the interpreter in the new location:
- Restore the Python interpreter to its original location.
- Uninstall the interpreter by using its installer, which clears the registry entries.
- Reinstall the interpreter at the new location.
- Restart Visual Studio, which should autodetect the new location in place of the old location.
This process ensures that the registry entries that identify the interpreter’s location, which Visual Studio uses, are properly updated. Using an installer also handles any other side effects that might exist.
Connect Anaconda With Visual Studio Code
We want to make sure VSC recognizes the Anaconda installation so it can access installed packages for autocomplete, syntax highlighting, and error checking. VSC should recognize Anaconda by default. If it does you will probably see something like “Python 3.9.7 (‘base’: conda)” in the lower-left corner of VSC (see image below).
If you don’t see this, or want to change to a different Anaconda environment (the example below is using the ‘base’ environment), use CTRL + SHIFT + P to open the settings palette (see image below).
Search for “Select Interpreter” and click on the “Python: Select Interpreter” option (see image below). If you have previously used the environment or interpreter it will probably appear in the palette drop-down. Otherwise you can add it as explained below.
Now click on “Enter Interpreter Path” then click on “Find”. This will open a File Explorer window where you can navigate to the location of the Python interpreter for another Anaconda environment. Navigate to the location of your Anaconda installation (C:\Users\your-user-name\anaconda3).
For the base Anaconda installation, select “python.exe”. To use a different Anaconda environment navigate to the “envs” folder, open the folder for an environment, and select “python.exe”. The lower-left corner of VSC should now update to show the name and Python version of the selected environment.
Once you have the proper Python interpreter and environment combination selected, code highlighting, autocomplete, and intellisense will be available in Visual Studio Code. If you’ve followed directions to this point you will have syntax highlighting and autocomplete working with VSC but you will not yet be able to run Python code from VSC. I’ll go through a few ways to do that next.
Manually identify an existing environment
Use the following steps to identify an environment installed in a nonstandard location.
-
In the Python Environments window (or from the Python toolbar), select Add Environment to open the Add environment dialog.
-
In the Add environment dialog, on the Existing environment tab, set the Environment field to
After you select the
value, more fields are added to the dialog.
-
Set the Prefix 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, select Add.
You can also review and modify details of the environment at any time in the Python Environments window.
-
In the Python Environments window, select the environment, and then select the Configure tab.
-
After you make changes, select the Apply command.
You can also remove the environment by using the Remove command. For more information, see Configure tab. This command isn’t available for autodetected environments.
Install Visual Studio Code and Anaconda
To start, download and install Visual Studio Code. When installing Visual Studio Code (VSC), you can keep all the default settings.
Once you have VSC installed, open VSC and install the Python extension and the Pylance extension (if they’re not already installed).
Before you continue, you might want to check out my FREE Python Quickstart course for the most up-to-date information.
Now install Anaconda (you can also install Miniconda, I actually recommend Miniconda). I like to use the default/recommended installation settings for Anaconda. I’ll describe these with images below.
To keep things simple, you can install Anaconda just for the current user.
Select your install location. I usually keep mine as the default. For Windows, this will probably be C:\Users\your-user-name\anaconda3. Remember the install location because you will need it later if you decide to add Anaconda to the path variable.
Now you will have the option to add Anaconda to the PATH environment variable. If you add Anaconda to the PATH variable it will most likely work with Visual Studio Code with no problems. However, I don’t like to do this because I frequently uninstall and reinstall Anaconda and it can cause problems if it is added to the PATH variable. I also use multiple python versions and distributions and adding them to the PATH variable can get complicated and confusing.
I do not recommend adding Anaconda to the PATH variable for now. Later in this article, I’ll show you how to do it manually in case you change your mind.
I do recommend registering Anaconda as your default Python distribution. Doing so will allow Visual Studio Code to locate the Anaconda Python distribution.
Click install and wait for the Anaconda installation to finish. This will take a little while.
Change the Workspace Settings
To use Anaconda with VSC you can also change the workspace settings so your project uses the Anaconda tools. To do this open the VSC settings and (CTRL + SHIFT + P) and type “settings”. Then select “Open Workspace Settings (JSON). This will create a new directory (.vscode) and an empty JSON file (settings.json) in your code directory. The JSON file will contain settings that pertain to your current project.
Edit
settings.json
by adding these lines of code. You may need to change the file paths slightly to reflect your system.
{ "python.pythonPath": "C:\\Users\\
\\python.exe", "python.terminal.activateEnvironment": true, "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe", }
This code will run the Windows Command Prompt with the Anaconda tools available so that you can run the Python code directly from VSC.
Right-click on the Python file you want to run in the VSC file explorer panel and select “Run Python File in Terminal”. This will open a new terminal window that is aware of your Anaconda environment and run the Python code.
The Python Environments window
The environments that Visual Studio knows about are displayed in the Python Environments window. To open the window, use one of the following methods:
- Select View > Other Windows > Python Environments.
- Right-click the Python Environments node for a project in Solution Explorer and select View All Python Environments.
The Python Environments window appears alongside Solution Explorer in Visual Studio:
Visual Studio looks for installed global environments by using the registry (following PEP 514), along with virtual environments and conda environments (see Types of environments). If you don’t see an expected environment in the list, see Manually identify an existing environment.
When you select an environment in the list, Visual Studio displays various properties and commands for that environment on the Overview tab of the Python Environments window, such as the interpreter location. The commands at the bottom of the Overview tab each open a command prompt with the interpreter running. For more information, see Python Environments window tab reference – Overview.
Use the dropdown list under the list of environments to switch to different tabs such as Packages and IntelliSense. These tabs are also described in the Python 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 the Make this the default environment for new projects command. Within the context of a project, you can always select a specific environment. For more information, see Select an environment for a project.
To the right of each listed environment, is a control that opens an Interactive 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, see Python 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.
What if no environments appear?
If you don’t see any environments in the Python 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, see Install 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 to manually identify an existing environment.
Types of environments
Visual Studio can work with global, virtual, and conda environments.
Global environments
Each Python installation maintains its own global environment. For example, Python 2.7, Python 3.6, Python 3.7, Anaconda 4.4.0, and so on. For more information, see Install 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 (within c:\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, see Select an environment for a project.
Virtual environments
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 a virtual 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 a requirements.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 includes requirements.txt file.
At any time within an open project, you can create a new virtual environment. In Solution Explorer, expand the project node, right-click Python Environments, and choose Add environment. In Add Environment, choose Virtual environment. For more information, see Create a virtual environment.
Visual Studio also provides a command to generate a requirements.txt file from a virtual environment, making it easy to recreate the environment on other computers. For more information, see Use virtual environments.
Conda environments
You can create a conda environment by using the
conda
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, see Install Python support in Visual Studio.
-
In the Python Environments window (or from the Python toolbar), select Add Environment to open the Add environment dialog.
-
In the Add environment dialog, select the Conda 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 command
python=,major>.
as in
python=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 the Make this the default environment for new projects in the Python Environments window. View in Python Environments window Specifies whether to show the Python 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 a
environments.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 the Python 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 the
conda info
command to see the locations of conda environment folders. You can then manually remove the subfolder for the environment from that location. -
Select Create.
You can monitor creation of the conda environment in the Output window. After creation completes, the output displays some command-line interface (CLI) instructions, such as
activate 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, see Select an environment for a project.
-
To install more packages in the environment, use the Packages tab on the Python 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 the
conda 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 in Manually identify an existing environment.
Visual Studio 2017 version 15.7 and later detects conda environments automatically and displays them in the Python Environments window as described in the next section.
Detect your environment
Visual Studio shows all known environments in the Python Environments window. It automatically detects updates to existing interpreters.
If Visual Studio doesn’t detect an installed environment, see Manually identify an existing environment.
If you want to provide new forms of detection for Python environments, see PTVS Environment Detection (github.com).
Registry entries
Visual Studio (all versions) automatically detects each installed Python interpreter and its environment by checking the registry according to PEP 514 – Python registration in the Windows registry. Python installations are typically found under the HKEY_LOCAL_MACHINE\SOFTWARE\Python (32-bit) and HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Python (64-bit) key within nodes for the distribution, such as PythonCore (CPython) and ContinuumAnalytics (Anaconda).
Phản hồi
Gửi và xem ý kiến phản hồi dành cho
Create and manage Python environments in Visual Studio
A Python 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 the Python 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 can select a specific environment rather than use the default.
-
For details on creating and using virtual environments for Python projects, see Use virtual environments.
-
If you want to install packages in an environment, refer to the Packages tab reference.
-
To install another Python interpreter, see Install 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 the Python Environments window and can be selected for projects.
Note
You can manage environments for Python code that open as a folder by selecting File > 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 the PythonSettings.json file in the Workspace .vs folder.
Keywords searched by users: visual studio 2019 anaconda
Categories: Tóm tắt 100 Visual Studio 2019 Anaconda
See more here: kientrucannam.vn
See more: https://kientrucannam.vn/vn/