Pylance is a fast, feature-rich language support extension for Python in Visual Studio Code, powered by the Pyright static type checker. It provides advanced type checking, code completion, code navigation, and other language features to enhance your Python development experience.
One of Pylance's powerful features is auto-import completions, which can significantly improve coding efficiency by automatically suggesting import statements as you type. However, this feature can affect the performance of the editor or might introduce too many suggestions, especially in large projects.
This guide explains what auto-import completions are, how the python.analysis.autoImportCompletions
setting affects them, and how you can customize Pylance to suit your workflow.
Auto-import completions are code completion suggestions provided by Pylance to automatically insert import statements for symbols (such as classes, functions, and constants) that are not currently imported into your code file.
When you start typing a symbol that is not yet imported, Pylance can suggest it along with an import statement, saving you the effort of manually adding the necessary imports at the top of your file.
Consider the following scenario:
def calculate_area():
circle = Circle(radius=5)
As you start typing Circle
, Pylance can suggest the Circle
class from mathlib.shapes
, even if you haven't imported it yet.
Selecting the suggestion automatically adds the import statement at the top of your file:
from mathlib.shapes import Circle
def calculate_area():
circle = Circle(radius=5)
This feature streamlines the coding process by reducing the need to manually write import statements.
The python.analysis.autoImportCompletions
setting in Pylance allows you to enable or disable auto-import completions. By adjusting this setting, you can control whether Pylance offers auto-import suggestions in the completions list.
true
: Enables auto-import completions. Pylance will suggest auto-imports for symbols as you type.false
(default): Disables auto-import completions. Pylance will not suggest symbols that are not already imported.
To adjust this setting in Visual Studio Code:
-
Open the Settings:
- Click on the gear icon in the lower-left corner and select Settings.
-
Search for the Setting:
- In the search bar at the top, type
python.analysis.autoImportCompletions
.
- In the search bar at the top, type
-
Modify the Setting:
- Check the box to set it to
true
(enable auto-import completions), or uncheck it to set it tofalse
(disable auto-import completions).
- Check the box to set it to
Alternatively, you can edit your settings.json
file directly:
-
Open the Settings (JSON):
- Open the Command Palette and select Preferences: Open Settings (JSON).
-
Add the Setting:
- Add or modify the following line in your
settings.json
file:
"python.analysis.autoImportCompletions": true
- Add or modify the following line in your
-
- Used to specify whether Pylance should index installed third party libraries and user files to improve features such as auto-import, add import, workspace symbols, etc.
-
python.analysis.includeAliasFromUserFiles
- Include alias symbols from user files. This will make alias symbols appear in features such as
add import
andauto import
.
- Include alias symbols from user files. This will make alias symbols appear in features such as
-
python.analysis.userFileIndexingLimit
- Maximum number of user files to index in the workspace.
-
python.analysis.packageIndexDepths
- Used to override how many levels under installed packages to index on a per package basis.
-
python.analysis.showOnlyDirectDependenciesInAutoImport
- Show only direct dependencies declared in
requirements.txt
orpyproject.toml
inauto import
suggestions, if they exist. This only affectsauto import
for completions. Theadd import
code action will continue to show all possible imports.
- Show only direct dependencies declared in
A: By default, Pylance indexes only the top-level modules of packages to optimize performance. If auto-import completions aren't suggesting symbols from deeper submodules, you may need to adjust the python.analysis.packageIndexDepths
setting to increase the indexing depth for those packages.
A: Enabling python.analysis.autoImportCompletions
can impact performance, especially in large projects or with packages that have many submodules. You can mitigate this by limiting the indexing depth or disabling auto-import completions if performance becomes an issue.
A: You can adjust the python.analysis.packageIndexDepths
setting by setting the depth
to 0
for packages you want to exclude:
"python.analysis.packageIndexDepths": [
{
"name": "unwanted_package",
"depth": 0
}
]
This prevents Pylance from indexing the specified package for auto-import completions.
A: Yes, by configuring the python.analysis.packageIndexDepths
setting, you can specify which packages to index and to what depth. Set depth
to a positive number for packages you want to include and 0
for others.
For more information on Pylance settings and customization, refer to the Pylance Settings and Customization documentation.
This document was generated with the assistance of AI and has been reviewed by humans for accuracy and completeness.