Python in Visual Studio Code – May 2018 Release
We are pleased to announce that the May 2018 release of the Python Extension for Visual Studio Code is now available from the marketplace and the gallery. You can download the Python extension from the marketplace, or install it directly from the extension gallery in Visual Studio Code. You can learn more about Python support in Visual Studio Code in the VS Code documentation.
In this release we have closed a total of 103 issues including support for the new and popular formatter Black, improvements to the experimental debugger and formatting as you type.
Support for Black Formatter
Black is a new code formatting tool for Python that was first released in March and has quickly gained popularity. Black has a single opinion about how Python code should be formatted, allowing you to easily achieve consistency across your codebase. The Python extension now supports using it as a formatter.
To enable the Black formatter, go into File > User Preferences > Settings, and put the following setting in your User Settings (for settings for all workspaces) or Workspace settings (for the current workspace/folder).
Then run the VS Code command “Format Document”. You will get a prompt to install the Black formatter:
Selecting Yes will install Black into the currently selected interpreter in VS Code. Once Black has finished installing, you will need to run the
Format Document command again to format your document.
In the below code example, we can see that black adds a blank line before functions, spaces around equals signs, and uses double quotation marks instead of single quotation marks:
If you want formatting to happen automatically when hitting save, you can add the following setting:
If you want to format Python 2.7 code, Black will need to run in a Python 3 environment. In that case, you can install black using
python3 –m pip install –upgrade black into a Python 3 interpreter/environment of your choice, and then set the
python.formatting.blackPath setting to point to the black command that was installed (on UNIX-based OSs you can typically find this with the command
Various Fixes and Enhancements
We have also added small enhancements and fixed issues requested by users that should improve your experience working with Python in Visual Studio Code. The full list of improvements is listed in our changelog, some notable improvements are:
- Improvements to testing: added a ‘Discover Unit Tests’ command (#1474) for discovering unit tests, removed error in the output window when running tests (#1529)
- Improvements to the Experimental Debugger: auto-enable jinja template debugging on *.jinja and *.j2 files (#1484), ensure debugged program is terminated when Stop debugging button is clicked. (#1345), support for attach/detach (#1255)
- Fixed syntax errors caused when using the editor.formatOnType setting is enabled (#1799)
- Ensure python environment activation works as expected within a multi-root workspace. (#1476)
- Fixed flask debugging configurations so that they work with the latest versions of flask (#1634)
- Ensure the display name of an interpreter does not get prefixed twice with the words Python. (#1651)
- `Go to Definition` now works for functions which have numbers that use `_` as a separator (as part of our Jedi 0.12.0 upgrade). (#180)
- Fixed rename refactor issue that removes the last line of the source file when the line is being refactored and source does not end with an EOL. (#695)
Be sure to download the Python extension for VS Code now to try out the above improvements. If you run into any issues be sure to file an issue on the Python VS Code GitHub page.