December 11th, 2024

Python in Visual Studio Code – December 2024 Release

jikang
Software Engineer

We’re excited to announce the December 2024 release of the Python, Pylance and Jupyter extensions for Visual Studio Code!

This release includes the following announcements:

  • Docstring generation features using Pylance and Copilot
  • Python Environments extension in preview
  • Pylance “full” language server mode

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

Docstring generation using Pylance and Copilot

A docstring is a string literal that appears right after the definition of a function, method, class, or module used to document the purpose and usage of the code it describes. Docstrings are essential for understanding and maintaining code, as they provide a clear explanation of what the code does, including parameters and return values. Writing docstrings manually can be time-consuming and prone to inconsistencies, however automating this process can ensure your code is well-documented, making it easier for others, and yourself, to understand and maintain. Automated docstring generation can also help enforce documentation standards across your codebase.

How to enable docstring generation

To start, open the Command Palette (Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (macOS)) and select Preferences: Open Settings (JSON).

Add the following Pylance setting to enable support for generating docstring templates automatically within VS Code:

  "python.analysis.supportDocstringTemplate": true

Add the following settings to enable generation with AI code actions:

    "python.analysis.aiCodeActions": {
        "generateDocstring": true
    }

Triggering docstring templates

  1. Define Your Function or Method:
    def my_function(param1: int, param2: str) -> bool:
        pass
  2. Add an Empty Docstring:
    • Directly below the function definition, add triple quotes for a docstring.
      def my_function(param1: int, param2: str) -> bool:
      """"""
      pass
  3. Place the Cursor Inside the Docstring:
    • Place your cursor between the triple quotes.
      def my_function(param1: int, param2: str) -> bool:
      """|  # Place cursor here """
      pass

When using Pylance, there are different ways you can request that docstrings templates are added to your code.

Using IntelliSense Completion

    • Press Ctrl+Space (Windows/Linux) or Cmd+Space (macOS) to trigger IntelliSense completion suggestions.

Using Code Actions

  1. Open the Context Menu:
    • Right-click inside the docstring or press Ctrl+. (Windows/Linux) or Cmd+. (macOS).
  2. Select Generate Docstring:
    • From the context menu, select Generate Docstring.
    • Pylance will suggest a docstring template based on the function signature.

Using Code Actions to Generate Docstring with Copilot

  1. Select Generate Docstring With Copilot:
    • From the context menu, select Generate Docstring With Copilot.
  2. Accept Suggestions:
    • GitHub Copilot chat will appear. Press Accept to take the suggestions or continue to iterate with Copilot.

Python Environments extension in preview

We’re excited to introduce the new Python Environments extension, now available in preview on the Marketplace.

This extension simplifies Python environment management with an Environments view accessible via the VS Code Activity Bar. Here you can create, delete, and switch between environments, and manage packages within the selected environment. It also uniquely supports specifying environments for specific files or entire Python projects, including multi-root and mono-repo scenarios.

Gif showing the Python environments extension managing environments for a multiroot project structure and managing packages in various environments.

 

By default, the extension uses the venv environment manager and pip package manager to determine how environments and packages are handled. You can customize these defaults by setting python-envs.defaultEnvManager and python-envs.defaultPackageManager to your preferred environment and package managers. Furthermore, if you have uv installed the extension will use it for quick and efficient environment creation and package installation.

Designed to integrate seamlessly with your preferred environment managers via various APIs, it supports Global Python interpreters, venv, and Conda out of the box. Developers can build extensions to add support for their favorite Python environment managers and integrate with our extension UI, enhancing functionality and user experience.

This extension is poised to eventually replace the environment functionality in the main Python extension and will be installed alongside it by default. In the meantime, you can download the Python Environments extensions from the Marketplace and use it in VS Code – Insiders (v1.96 or greater) and with the pre-release version of the Python extension (v2024.23 or greater). We are looking forward to hearing your feedback on improvements by opening issues in the vscode-python-environments repository.

Pylance “full” language server mode

The python.analysis.languageServerMode setting now supports full mode, allowing you to take advantage of the complete range of Pylance’s functionality and the most comprehensive IntelliSense experience. It’s worth noting that this comes at the cost of lower performance, as it can cause Pylance to be resource-intensive, particularly in large codebases.

The python.analysis.languageServerMode setting now changes the default values of the following settings, depending on whether it’s set to light, default or full:

Setting light default full
python.analysis.exclude [“**”] [] []
python.analysis.useLibraryCodeForTypes false true true
python.analysis.enablePytestSupport false true true
python.analysis.indexing false true true
python.analysis.autoImportCompletions false false true
python.analysis.showOnlyDirectDependenciesInAutoImport false false true
python.analysis.packageIndexDepths [ { "name": "sklearn", "depth": 2 }, { "name": "matplotlib", "depth": 2 }, { "name": "scipy", "depth": 2 }, { "name": "django", "depth": 2 }, { "name": "flask", "depth": 2 }, { "name": "fastapi", "depth": 2 } ] | [ { "name": "sklearn", "depth": 2 }, { "name": "matplotlib", "depth": 2 }, { "name": "scipy", "depth": 2 }, { "name": "django", "depth": 2 }, { "name": "flask", "depth": 2 }, { "name": "fastapi", "depth": 2 } ] | { "name": "", "depth": 4, "includeAllSymbols": true }
python.analysis.regenerateStdLibIndices false false true
python.analysis.userFileIndexingLimit 2000 2000 -1
python.analysis.includeAliasesFromUserFiles false false true
python.analysis.functionReturnTypes false false true
python.analysis.pytestParameters false false true
python.analysis.supportRestructuredText false false true
python.analysis.supportDocstringTemplate false false true

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:

  • The testing rewrite nearing default status: This release addresses the final known issue in the testing rewrite, and we plan to turn off the rewrite experiment and set it to the default in early 2025
  • Python Native REPL handles window reload in @vscode-python#24021
  • Leave focus on editor after Smart Send to Native REPL in @vscode-python#23843
  • Add error communication around dynamic adapter activation in @vscode-python#23234
  • Pytest --rootdir argument for pytest is now dynamically adjusted based on the presence of a python.testing.cwd setting in your workspace in @vscode-python#9553
  • Add support for interpreter paths with spaces in the debugger extension in @vscode-python-debugger#233
  • pytest-describe plugin is supported with test detection and execution in the UI in @vscode-python#21705
  • Test coverage support updated to handle NoSource exceptions in @vscode-python#24366
  • Restarting a test debugging session now reruns only the specified tests in @vscode-python-debugger#338
  • The testing rewrite now leverages FIFO instead of UDS for inter-process communication allowing users to harness pytest plugins like pytest_socket in their own testing design in @vscode-python#23279

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.

Author

jikang
Software Engineer

0 comments

'; block.insertAdjacentElement('beforebegin', codeheader); let button = codeheader.querySelector('.copy-button'); button.addEventListener("click", async () => { let blockToCopy = block; await copyCode(blockToCopy, button); }); } }); async function copyCode(blockToCopy, button) { let code = blockToCopy.querySelector("code"); let text = ''; if (code) { text = code.innerText; } else { text = blockToCopy.innerText; } try { await navigator.clipboard.writeText(text); } catch (err) { console.error('Failed to copy:', err); } button.innerText = "Copied"; setTimeout(() => { button.innerHTML = '' + svgCodeIcon + ' Copy'; }, 1400); }