Python in Visual Studio Code
Working with Python in Visual Studio Code, using the Microsoft Python extension, is simple, fun, and productive. The extension works with different Python interpreters as well as Anaconda. It leverages all of VS Code's power to provide auto complete and IntelliSense, linting, debugging, and unit testing, along with the ability to easily switch between Python environments, including virtual and conda environments.
Install Python and the Python extension
The tutorial guides you through installing Python and using the extension.
Autocomplete and IntelliSense
The Python extension supports code completion and Intellisense. Intellisense is a general term for a number of features, including intelligent code completion (in-context method and variable suggestions) across all your files and for built-in and third-party modules.
IntelliSense quickly shows methods, class members, and documentation as you type, and you can trigger completions at any time with ⌃Space (Windows, Linux Ctrl+Space).
Linting analyzes your Python code for potential errors, making it easy to navigate to and correct different problems.
The Python extension can apply a number of different linters including Pylint, Pep8, Flake8, mypy, pydocstule, prospector, and pylama. See Linting.
Snippets take productivity to the next level. You can configure your own snippets and use snippets provided by an extension. Snippets appear in the same way as code completion ⌃Space (Windows, Linux Ctrl+Space).
The Python extension automatically detects Python interpreters that are installed in standard locations. It also detects conda environments as well as virtual environments in the workspace folder. See Environments.
The current environment is shown on the left side of the Visual Studio Code status bar:
This environment is used for IntelliSense, auto-completions, linting, formatting, and any other language-related feature other than debugging. It is also activated when you use run Python in a terminal.
To change the current interpreter, which includes switching to conda or virtual environments, click the interpreter on the status bar or use the Python: Select Interpreter command.
VS Code then prompts you with a list of detected environments as well as any you've added manually to your user settings (see Environments).
Run Python in the Terminal
The Python extension provides shortcuts to quickly run Python code in the current interpreter:
- From the editor: right-click anywhere in the editor and select Run Python File in Terminal. If invoked on a selection, only that selection is run.
- From Explorer: right-click a Python file and select Run Python File in Terminal.
You can also use the Python: Create Terminal command to create a terminal with the current environment activated.
The Python extension supports unit testing with the unittest, pytest, and nose test frameworks.
To run unit tests, you enable one of the frameworks in settings. Each framework also has specific settings, such as arguments that identify paths and patterns for test discovery.
Once discovered, VS Code provides a variety of commands (on the status bar, the command palette, and elsewhere) to run and debug tests, including ability to run individual test files and individual methods.
The Python extension provides a wide variety of settings for its various features. These are described on their relevant topics, such as Editing code, Linting, Debugging, and Unit Testing. The complete list is found in the Settings reference.
Other popular Python extensions
Additional Python language support can be added to to VS Code by installing other popular Python extensions. For Jupyter support, we recommend the "Jupyter" extension from Don Jayamanne.
- Open the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)).
- Filter the extension list by typing 'python'.
The extensions shown above are dynamically queried. Click on an extension tile above to read the description and reviews to decide which extension is best for you. See more in the Marketplace.