Python in Visual Studio 2019 Preview 2
Today we are releasing Visual Studio 2019 Preview 2, which contains new features for Python developers to improve the experience for managing Python environments and enable you to work with Python code without having to create a Python project. We’ve also enabled Python support for Visual Studio Live Share.
We’ll take a closer look at these new features in the rest of this post.
Creating Python Environments
To make it easier for you to create virtual or conda Python environments for your project, the ability to create Python environments has been moved from the Python environments window to a new Add environment dialog that can be opened from various parts of Visual Studio. This improves discoverability and enables new capabilities such as the ability to create conda environments on-demand and support for Open Folder described later in this post.
For example, when opening a project that contains a requirements.txt file or environment.yml but no virtual environment or conda environment is found, you will be prompted to create an environment with a notification:
In this case clicking on ‘Create virtual environment’ will show a new Add environment dialog box, pre-configured to create a new virtual environment using the provided requirements.txt file:
You can also use the Add environment dialog to create a conda environment, using an environment.yml file, or by specifying a list of packages to install:
The dialog also allows you to add existing virtual environments on your machine, or to install new versions of Python.
After clicking on Create, you will see a progress notification in the status bar, and you can click the blue link to view progress in the Output window:
You can continue working while the environment is being created.
Switching from Anaconda to Miniconda
Previous versions of Visual Studio allowed you to install Anaconda through the Visual Studio Installer, and while this enabled you to easily acquire Python data science packages, this resulted in large Visual Studio installation times and sometimes caused reliability issues when upgrading versions of Visual Studio. To address these problems, Anaconda has been removed in favor of the much smaller Miniconda, which is now installed as a default optional component in for the Python workload:
Miniconda allows you to create conda environments on-demand using the Add environment dialog. If you still want to continue using a full install of Anaconda, you can install Anaconda yourself and continue working with Anaconda by selecting your Anaconda install as the active Python environment. Note that if both the Visual Studio bundled Miniconda and Anaconda are installed, we will use Miniconda to create conda environments. If you prefer to use your own conda version you can specify the path to conda.exe in Tools > Options > Python > Conda.
Python Toolbar and Open Folder Support
In previous versions of Visual Studio we required you to create a Python project in order to work with Python code. We have added a new Python toolbar that allows you to work with Python code without having to create or open a Python project. The toolbar allows you to switch between Python environments, add new Python environments, or manage Python packages installed in the current environment:
The Python toolbar will appear whenever a Python file is open and allows you to select your Python interpreter when working with files in Open Folder workspaces or Python files included in C++ or C# projects.
In the case of Open Folder, your selection is stored in the .vs/PythonSettings.json file so that the same environment is selected the next time you open Visual Studio. By default, the debugger will debug the currently opened Python file, and will run the script using the currently selected Python environment:
To customize your debug settings, you can right-click a Python file and select “Debug and Launch Settings”:
This will generate a launch.vs.json file with Python settings which can be used to customize debug settings:
Note that in Preview 2, the editor and interactive window do not use the currently selected Python environment when using Open Folder. This functionality will be added in future previews of Visual Studio 2019.
Live Share Support for Python
In this release you can now use Visual Studio Live Share with Python files. Previously, you could only use Live Share with Python by hosting a session with Visual Studio Code. You can initiate a Live Share session by clicking the Live Share button on the upper right corner of Visual Studio:
Users who join your live share session will be able to see your Python files, see IntelliSense from your selected Python environment, and collaboratively debug through Python code:
Try it out!
Be sure to download the Visual Studio 2019 Preview 2, install the Python Workload, and give feedback on Visual Studio Developer Community.
At some point between this blog post and the recent unveiling of VS2019, the decision was made that if you check the box to include Python with your installation of VS1029, it will automagically move Python from its environment to the VS2019 environment and NOT update the PATH to reflect the move, breaking pip and everything else that relies on being able to find python.exe from the PATH. Oh, and it doesn’t alert you it’s doing this either.