Editing Python in Visual Studio Code
Visual Studio Code is a powerful editing tool for Python source code. The editor includes various features to help you be productive when writing code. For more information about editing in Visual Studio Code, see Basic Editing and Code Navigation.
In this overview, we will describe the specific editing features provided by the Python extension, including steps on how to customize these features via user and workspace settings.
Autocomplete and IntelliSense
IntelliSense is a general term for code editing features that relate to code completion. Take a moment to look at the example below. When print is typed, notice how IntelliSense populates auto-completion options. The user is also given a list of options when they begin to type the variable named greeting.
Autocomplete and IntelliSense are provided for all files within the current working folder. They're also available for Python packages that are installed in standard locations.
Pylance is the default language server for Python in VS Code, and is installed alongside the Python extension to provide IntelliSense features.
Pylance is based on Microsoft’s Pyright static type checking tool, leveraging type stubs (.pyi
files) and lazy type inferencing to provide a highly-performant development experience.
For more on IntelliSense generally, see IntelliSense.
Tip: Check out the IntelliCode extension for VS Code. IntelliCode provides a set of AI-assisted capabilities for IntelliSense in Python, such as inferring the most relevant auto-completions based on the current code context. For more information, see the IntelliCode for VS Code FAQ.
Customize IntelliSense behavior
Enabling the full set of IntelliSense features by default could end up making your development experience feel slower, so the Python extension enables a minimum set of features that allow you to be productive while still having a performant experience. However, you can customize the behavior of the analysis engine to your liking through multiple settings.
Enable Auto Imports
Pylance offers auto import suggestions for modules in your workspace and for packages you installed in your environment. As you type in the editor, you might get completion suggestions. When you accept the suggestion, auto import automatically adds the corresponding import statement to your file.
You can enable auto imports by setting python.analysis.autoImportCompletions
to true
in your settings. By default, auto imports are disabled.
Enable IntelliSense for custom package locations
To enable IntelliSense for packages that are installed in non-standard locations, add those locations to the python.analysis.extraPaths
collection in your settings.json
file (the default collection is empty). For example, you might have Google App Engine installed in custom locations, specified in app.yaml
if you use Flask. In this case, you'd specify those locations as follows:
Windows:
"python.analysis.extraPaths": [
"C:/Program Files (x86)/Google/google_appengine",
"C:/Program Files (x86)/Google/google_appengine/lib/flask-0.12"]
macOS/Linux:
"python.analysis.extraPaths": [
"~/.local/lib/Google/google_appengine",
"~/.local/lib/Google/google_appengine/lib/flask-0.12" ]
For the full list of available IntelliSense controls, you can reference the Python extension code analysis settings and autocomplete settings.
You can also customize the general behavior of autocomplete and IntelliSense, even disable the features completely. You can learn more in Customizing IntelliSense.