Python in Visual Studio Code – November 2023 Release

Courtney Webster

We’re excited to announce the November 2023 release of the Python and Jupyter extensions for Visual Studio Code!

This release includes the following announcements:

  • Improvements to Shift + Enter run line in terminal
  • Deprecated built-in linting and formatting features
  • Improvements to Python linting extensions
  • Reconfiguration of test output
  • Virtual environment deactivation help
  • Announcing Python in VS Code release videos

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

Improvements to Shift + Enter run line in terminal

The Python extension is working on improving the behavior of sending code to the Python REPL (Shift+Enter) when no code has been explicitly selected to run. Previously, when you placed your cursor on a line of Python code and pressed Shift+Enter, the Python extension would send the exact line contents to the REPL, even if it would fail, for example, due to being part of a multi-line command.

With our new experimental Smart Send feature, the Python extension will send the smallest runnable block of code surrounding the line where your cursor is to the REPL for execution. This ensures that only complete and executable sections of code are sent to the REPL. The cursor will also be automatically placed to the next executable line, to provide a smooth experience when executing multiple chunks iteratively.

To try it out, you can add the following User setting: "python.experiments.optInto: ["pythonREPLSmartSend"]. While this feature is currently behind an experiment, we expect it to be the default behavior in the future. If you have feedback or suggestions on how we can further improve this feature, please let us know!

Gif showing new Shift+Enter functionality when sending code to the Python terminal.

Deprecated built-in linting and formatting features

With all the ongoing work and improvements we have been making to the linting and formatting extensions in VS Code for the last year, we have deprecated the built-in linting and formatting features that are shipped within the Python extension. This includes all the linting and formatting commands as well as settings (python.linting.* and python.formatting.*). We recommend you to remove these deprecated settings if you are still using them, and use the linting and formatting extensions instead, as they offer a more configurable and flexible experience.

If you were using a linter or formatter that does not yet have an extension, you can create linter or formatter VS Code extensions for your favorite Python tool! Check out our Python Tools Extension Template for a quick start.

Furthermore, we would like to highlight the community-contributed Ruff extension. Ruff is a Python linter written in Rust and it supports various linters such as pyflakes, pycodestyle, pydocstyle, and more! They have also recently enabled support for using Ruff as a formatter in VS Code ("[python]": { "editor.defaultFormatter": "charliermarsh.ruff" }). Try it out for your Python linting and formatting support in VS Code!

Improvements to Python linting extensions

We have made several improvements to the linting extensions our team supports to allow for an even more configurable and flexible experience with your favorite Python tools.

The Pylint, Mypy and Flake8 extensions now offer settings that allow you to specify glob patterns for files that you wish to exclude from linting. This can be useful if you are working with a large codebase with many sub-projects, and want to exclude certain folders from being linted.

These settings are "pylint.ignorePatterns", "mypy-type-checker.ignorePatterns" and "flake8.ignorePatterns", respectively.

These extensions also support cwd settings, which allows you to specify the working directory for the linter. This setting has been updated to support the variable ${fileDirname}, so the working directory can be dynamically set to the parent folder of the file you have open in the editor. This can be particularly useful if you are working with mono-repos, and want the linter’s working directory to be dynamically updated as you open files from different sub-projects. These settings are "pylint.cwd", "mypy-type-checker.cwd" and "flake8.cwd", respectively.

We have also changed the default value of the "mypy-type-checker.preferDaemon", only applicable to the Mypy extension. Previously, it was set to true, which meant that the Mypy daemon would be used by default. After receiving feedback, we have changed the default value to false. If you are wondering which value would be best for you, our recommendation is to use the Mypy daemon if you enabled the Mypy reporting scope to be the entire workspace ("mypy-type-checker.reportingScope": "workspace") for performance reasons. Otherwise, if the reporting scope is set to the current file, we recommend you use the Mypy executable that is shipped with the extension.

Reconfiguration of test output

We’ve made significant improvements to how you can view and interact with the Test Output in the Python extension when the pythonTestAdapter experiment is enabled, which we announced a few months ago. Previously, output from test discovery and execution was inconsistently scattered across the Python Test Log output channel and the Test Results panel, with some information being duplicated in both places. To consolidate the experience, we are now displaying output related to test execution in the Test Results panel, and test discovery in the Python output channel. To learn more see our related wiki.

This new approach also supports colorization in the output if you are using Pytest and set "python.testing.pytestArgs": ["--color=yes"] in your Workspace settings.json. Colorization only works for test execution in the Test Results panel and will not work for discovery or for output in the Python Test Log panel.

Image showing new Python test output configuration.

There is also a new button that allows you to open the Test Logs more easily from the Test Explorer view when test discovery errors occur.

Image showing the "Show Output" on test discovery errors.

Virtual environment deactivation help

A couple of months ago we announced a new feature behind an experiment that enables terminal activation using environment variables, which aims to enhance your development workflow by automatically activating the selected environment in the terminal without the need for explicit activation commands. However, since there are no explicit activation scripts working, the deactivate command was no longer working when this experiment was enabled.

The Python extension will now detect when you attempt to run the deactivate command and show a helper notification that will guide you on how to add scripts for your shell to enable the command to work again when the environment is activated through environment variables. It also offers a button to more conveniently open your shell profile file for you to add the necessary scripts.

Image showing deactivate prompt help.

You can find the full documentation on how to add the necessary scripts for your shell here.

If you are not in the experiment and would like to try out this feature, you can add the following User setting: "python.experiments.optInto: ["pythonTerminalEnvVarActivation"].

Python in VS Code release videos

Python Community Advocate, Dawn Wages walks through key release news and enhancements in the Python in VS Code release video! These short videos will come out with all subsequent releases on the Visual Studio Code YouTube channel highlighting key changes and enhancements along with video content demonstrating how to interact with these new features!

 

For more Python news and community engagement, check out the Python Pulse hosted by Dawn Wages every second Friday of the month!

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:

  • A new FastAPI tutorial has been added to our docs! Learn how to get the most out of VS Code and the Python extension when developing a FastAPI application.
  • The Python extension now displays a notification to help create virtual environments when you attempt to run or debug a Python file within a workspace that contains a dependency file (such as requirements.txt or requirements/*.txt) and there is no current .venv or .conda folder. This notification provides a quick and easy way to create a new virtual environment using the Python: Create Environment command. This setting can be disabled to setting python.createEnvironment.trigger to off.
  • The Python Debugger extension now has platform-specific versions, which means only the necessary files for your platform will be installed on update. This reduces the size of the extension and helps with improving startup time.
  • Tensorboard functionality has now been moved out of the Python extension and into the standalone Tensorboard extension. Feedback can be provided in the Tensorboard extension GitHub repo.
  • You can now enable and disable Pylance auto import completions more easily through the language status icon on the Status Bar (pylance-release#3894)
  • IntelliSense support for __setattr__, __delattr__ and __getattribute__ when using Pylance (pylance-release#4786)

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

Call for Community Feedback

As we are planning and prioritizing future work, we value your feedback! Below are a few issues we would love feedback on:

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.

0 comments

Discussion is closed.

Feedback usabilla icon