Python in Visual Studio Code – December 2024 Release
We’re excited to announce the December 2024 release of thePython,Pylance andJupyter 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 thePython,Jupyter andPylance 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) orCmd+Shift+P
(macOS)) and selectPreferences: Open Settings (JSON).
Add the followingPylance 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
- Define Your Function or Method:
def my_function(param1: int, param2: str) -> bool: pass
- Add an Empty Docstring:
- Directly below the function definition, add triple quotes for a docstring.
def my_function(param1: int, param2: str) -> bool:""""""pass
- Directly below the function definition, add triple quotes for a docstring.
- 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
- Place your cursor between the triple quotes.
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) orCmd+Space
(macOS) to trigger IntelliSense completion suggestions.
- Press
Using Code Actions
- Open the Context Menu:
- Right-click inside the docstring or press
Ctrl+.
(Windows/Linux) orCmd+.
(macOS).
- Right-click inside the docstring or press
- Select Generate Docstring:
- From the context menu, selectGenerate Docstring.
- Pylance will suggest a docstring template based on the function signature.
Using Code Actions to Generate Docstring with Copilot
- Select Generate Docstring With Copilot:
- From the context menu, selectGenerate Docstring With Copilot.
- Accept Suggestions:
- GitHub Copilot chat will appear. PressAccept to take the suggestions or continue to iterate with Copilot.
Python Environments extension in preview
We’re excited to introduce the newPython 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.
By default, the extension uses thevenv
environment manager andpip
package manager to determine how environments and packages are handled. You can customize these defaults by settingpython-envs.defaultEnvManager
andpython-envs.defaultPackageManager
to your preferred environment and package managers. Furthermore, if you haveuv
installed the extension will use it for quick and efficient environment creation and package installation.
Designed to integrate seamlessly with your preferred environment managers viavarious 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 thePython Environments extensions from the Marketplace and use it inVS 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 thevscode-python-environments repository.
Pylance “full” language server mode
Thepython.analysis.languageServerMode
setting now supportsfull
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.
Thepython.analysis.languageServerMode
setting now changes the default values of the following settings, depending on whether it’s set tolight
,default
orfull
:
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 addressesthe 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 apython.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:
- @joar Ruff 0.8.0 fixes in@vscode-python#24488
- @renan-r-santos Add native pixi locator in@vscode-python#244420
- @tomoki Fix the wrong Content-Length in
python-server.py
for non-ascii characters in@vscode-python#24480
Try out these new improvements by downloading thePython extension and theJupyter 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 aboutPython support in Visual Studio Code in the documentation. If you run into any problems or have suggestions,please file an issue on thePython VS Code GitHub page.