Python in Visual Studio 2017 version 15.7
Note: This post was originally published with Visual Studio 15.7 Preview 4, it has been updated to reflect the latest as of Visual Studio 15.7 RTM.
We have released the 15.7 update to Visual Studio 2017. You will see a notification in Visual Studio within the next few days, or you can download the new installer from visualstudio.com.
In this post, we’re going to look at some of the new features we have added for Python developers: faster debugging, Conda environments, type hints and MyPy support. If you encounter issues with the new features, please use the Report a Problem tool to let us know.
This release includes an opt-in preview of new version of our ptvsd debug engine based on PyDevD, which we expect to be much faster than the previous version. To use the new version check Use Experimental Debugger in Tools > Options > Python > Experimental.
Most of the features you used in the previous version of the debugger are still available, but the following features are not yet supported:
- Set Next Statement
- Just My Code
If you want to use these features, you can stay on the previous debugger version.
The following cases debugging still works but we fall back to the previous version of the debugger:
- IronPython debugging
- Attach to process
- Debug unit tests
- Mixed-mode debugging
For remote debugging you will need to start ptvsd on the remote server using the command:
py -3 -m ptvsd --server --port <port_num> --file main.py
We have also made a preview of the new ptvsd available in the Python extension for Visual Studio Code, which continues to keep our debugging capabilities consistent across Visual Studio and Visual Studio Code.
IntelliSense for Type Hints
As type hints in Python continue to gain popularity, we want to make sure you have easy access to the best tools to take advantage of them. These will help you improve your code quality and help make refactoring safer.
In this release we have added support for type hints in our IntelliSense. When you add type hints to parameters or variables they’ll be shown in hover tooltips. In the example below a Vector type is declared as a list of floats, and the scale() method is decorated with types to indicate the parameters and return types. Hovering over the scale() method when calling it shows the expected parameters and return type:
Using type hints you can also declare class attributes and their types, which is handy if those attributes are dynamically added later. Below we declare that the Employee class has a name and an id, and that information is then available in IntelliSense when using variables of type Employee:
In the next section, we’ll see how you can run MyPy to use these type hints to validate your code and detect errors.
Using MyPy with Type Hints
To fully validate your code against type hints, we recommend using MyPy. MyPy is the industry standard tool for validating type hints throughout your entire project. As a separate tool, you can easily configure it to run in your build system as well as the development environment, but it is also useful to have it be easily accessible while developing.
To run MyPy against your project, right-click on the project in Solution Explorer and find it under the Python menu.
This will install it if necessary, and run against every file included in your project. Warnings will be displayed in the Error List window and selecting an item will take you directly to the location in your sources.
By default, you may see many more warnings than you are prepared to fix straight away. To configure the files and warnings you see, use the Add New Item command and select the MyPy Configuration file. This file is automatically detected by MyPy and contains various filters and settings. See the MyPy documentation for full information about configuration options.
You can now create and use Conda environments as well as manage packages for your Conda environments using pip or Conda.
To manage or use Conda environments from Visual Studio, you’ll need Anaconda or Miniconda. You can install Anaconda directly from the Visual Studio installer or get it separately if you’d rather manage the installation yourself.
Note: There are known issues when using older versions of the conda package (4.4.8 or later is recommended). The latest distributions of Anaconda/Miniconda have the necessary version of the conda package.
You can create a new Conda environment using the Python Environments window, using a base Python from version 2.6 to 3.6.
Any environments created using Visual Studio or the Conda tool will be detected and listed in the Python Environments window automatically. You can open interactive windows for these environments, assign them in projects or make them your default environment. You can also delete them using Visual Studio.
To manage packages, you have the option of using either Conda or Pip package manager from the Python Environments window.
The user interface for both package manager is the same. It displays the list of installed packages and let you update or uninstall them.
You can also search for available Conda packages.
Solution Explorer also displays packages for each environment referenced in your project. It lists either Conda or pip packages, picking the most appropriate for the type of environment.
Be sure to download the latest preview of Visual Studio and try out the above improvements. If you encounter any issues, please use the Report a Problem tool to let us know (this can be found under Help, Send Feedback) or continue to use our GitHub page. Follow our Python blog to make sure you hear about our updates first, and thank you for using Visual Studio!