Python in Visual Studio Code – August 2022 Release

Kartik Raj

We’re excited to announce that the August 2022 release of the Python and Jupyter extensions for Visual Studio Code are now available!

This release includes the following improvements:

  • Automatic debug configurations for web apps
  • Improvements to the Getting Started Experience
  • Setting to configure the display of the interpreter information on the status bar
  • A new Python Tools extension template
  • Remove all unused imports with Pylance
  • Double click to accept type hints with Pylance
  • Copy output image in Jupyter cells
  • Go to most recently failed cell

If you’re interested, you can check the full list of improvements in our changelogs for the Python, Jupyter and Pylance extensions.

Automatic debug configurations for web apps

To simplify the process of setting up VS Code to run and/or debug your web applications, this release includes new automatic debug configurations for projects that use Flask, Django or FastAPI.

When you open the Run and Debug view in VS Code (Ctrl + Shift + D or ⌘+ ⇧ + D) and there is no debugger configuration (i.e., no launch.json file) on your workspace, you will see an “show all automatic debug configurations” option. If your project is a web application that follows the format expected by Flask, Django or FastAPI, once you click on that option you will be able to see debug configurations that were dynamically created to execute and debug your project. Now you can just select it to start catching all the bugs!

Debugging a FastAPI application by selecting an dynamically created debug configuration for FastAPI. Theme: Pink-Cat-Book

Improvements to the Getting Started Experience

The Python extension walkthrough has been improved to more easily guide users to installing Python when needed, and selecting it in VS Code. When Python is not installed on a Windows machine, selecting the “Install Python” button from the walkthrough will open the Python app in the Microsoft Store. When on Linux or macOS, the displayed installation instructions will be automatically run in the terminal.

Another improvement related to the getting started experience is that notifications for selecting the interpreter are now only shown when an interpreter is required (e.g. when running or debugging a file, or a tool such as a linter or formatter), and no longer on start-up. Other notification prompts have also been improved to be more precise about errors and suggested changes.

Python extension walkthrough with a create Python file button.

Python interpreter display on the status bar

We introduced a new User setting called “python.interpreter.infoVisibility”, which controls when the selected interpreter information is displayed in the status bar. You can set "python.interpreter.infoVisibility": “always” on your User settings (Preferences > Command Palette… > Open User Settings (JSON)) to always display it regardless of what file is opened. By default, it’s only displayed when Python-related files are open on the editor ("python.interpreter.infoVisibility": “onPythonRelated”). If you’d like to hide it completely, set "python.interpreter.infoVisibility": “never”.

A warning status was also added to indicate situations when the selected interpreter is invalid:

A warning icon and a different background color is displayed on the status bar when the selected Python interpreter is invalid.

Python Tools extension template

With all the work we have been doing to enable formatting and linting with Black, pylint, and isort to work behind the Language Server Protocol, we realized how simple we could make it to create an extension for mostly any Python linter or formatter without the need to require TypeScript knowledge (which is the language VS Code and its extensions are written in).

To that end, our team has created a template that allows you to create a VS Code extension for your favorite Python linter or formatter by changing mostly Python code (unless you’re planning to add more settings or custom logic to it – then you’ll need to work with a bit of TypeScript).

To try it out, you can follow the instructions in the repository. If at the end you’d like to publish your new extension (and maintain it, after all with great power comes great responsibility), you can follow the steps on the “Publishing Extensions” documentation.

And If you have any feedback on this new template, please file an issue on the GitHub repo!

Remove all unused imports

Pylance now offers a code action for removing all unused imports when there is more than one library that is being imported but not used or called anywhere in the code. To try it out, open a Python file with such statements and trigger the code action called “Remove all unused imports”: Code action for removing all unused imports displayed when more than one library is imported but not used in the code.Theme: Pink-Cat-Book

You can also configure VS Code to run all available “fix all” code actions when you save your Python files by adding the following configuration to your User settings:

"[python]": {
    "editor.codeActionsOnSave": {
        "source.fixAll": true

Double click to accept Type Hints

In the July release, we have added support for variable types and return types inlay hints when using Pylance. In this release, we’re introducing support to accepting the suggested annotations to your code. You can now double click on a hint to add the suggestion to your code:

Adding type suggestions given by Pylance on the editor by double clicking on them.

To learn more about Inlay Type hints and for some tips on how you can best leverage this feature, check out Jay Miller’s explanatory video and blog post on how to “Make Inlay Type Hints in Python Appear/Disappear”.

Copy images to clipboard from Jupyter Notebooks

The new release of the Jupyter extension allows you to copy images from Jupyter notebooks output cells to the clipboard so you can more easily share them outside of VS Code!

To try it out, make sure you change the output presentation to png, by clicking on the icon on the left side of the image you’re trying to copy:

Selecting image/png as the rendered for a Jupyter notebook output image.

Then hover over the image and select the copy button that shows up to the right:

Copy icon displayed on the right of the Jupyter notebook image output

And now you can paste it into your e-mails, documents, reports and even Paint!

Go to most recently failed cell

Another improvement introduced in the latest Jupyter extension release is the ability to navigate to the most recently failed cell. When you run all your cells and one fails, you can click on the “Go To” button on the top of the editor and you will be taken to the one that halted the group execution.

Go to failed cell button displayed on the Jupyter Notebook toolbar.

Other Changes and Enhancements

We have also added small enhancements and fixed issues requested by users that should improve your experience working with Python and Jupyter Notebooks in Visual Studio Code. Some notable changes include:

  • There’s a new setting in the Pylint and Black extensions that allow you to specify a interpreter command to launch the LSP server (vscode-python#19303).
  • We now have a new command (python.triggerEnvSelection) that other extensions can use to trigger an error notification to select a valid Python interpreter (vscode-python#19450).
  • We improved support for magic commands in the interactive window when using Pylance (pylance-release#2894).
  • Type analysis performance has been improved with Pylance(pylance-release#2881).
  • The Jupyter extension now has diagnostic messages with code actions for files that could interfere with execution of Python Kernels (vscode-jupyter#10924).
  • Interactive Window sessions can now be restored across VS Code window reloads by turning on the setting restore (vscode-jupyter#6420).

We would also like to extend special thanks to this month’s contributors:

Try out these new improvements by downloading the Python extension and the Jupyter extension from the Marketplace, or install them directly from the extensions view in Visual Studio Code (Ctrl + Shift + X or ⌘ + ⇧ + X). You can learn more about Python support in Visual Studio Code in the documentation. If you run into any problems or have suggestions, please file an issue on the Python VS Code GitHub page.