How to Configure VSCode to Write and Run Python Code
Prerequisites
Before you start this article, make sure Python is installed correctly; you can check the article How to Install Python on UNIX/Linux/Windows/macOS for information.
Install VSCode
VSCode (Visual Studio Code, VS Code) is a powerful and effective development tool that supports writing and debugging code in multiple languages, including Python, by installing relevant extensions.
The official website provides different versions of installers and compressed packages for developers to use VSCode on Linux/Windows/macOS, you can download the corresponding installer or compressed package according to your own needs, the installation process is relatively simple, so I’ll skip it here.
Install the VSCode Python Extension
To run and debug Python code in VSCode, you have the option of installing the VSCode extension Python
(ms-python.python), developed by Microsoft, which provides the common functionality needed for Python development. The extension can be found through the Extension Panel search function, and you need to choose to install or enable it as appropriate, if it is already enabled you don’t need to do anything.
How do I configure the version of the Python interpreter used by VSCode?
If you have more than one version of Python, you can choose one of them as the Python interpreter for VSCode in the following way.
Open VSCode’s Command Palette using the Command Palette keyboard shortcut (defaults to Ctrl+Shift+P on Linux/Windows and ⇧+⌘+P on macOS), and enter and execute the command Python: Select Interpreter
to add and select a particular version of the Python interpreter.
Python Extension requires other extensions for more functionality
Since the extension Python
does not have specific implementations of certain features, it is often necessary to go on to install some other extensions after installing the extension Python
. For example, you can install Pylance
, which implements code smart hints and completions, autopep8
or Black Formatter
, which implements code formatting, and Pylint
or Flake8
, which implements code error correction and quality checking.
These extensions are installed similarly to the extension Python
, some of them may require a restart of VSCode to take effect.
Install the VSCode Pylance Extension
The extension Pylance
(ms-python.vscode-pylance) provides VSCode with diagnostic analysis, smart hints, and completions of Python code. Since Pylance
is the default language server chosen by the extension Python
, it is usually installed with the extension Python
.
How do I specify the default Python language server for VSCode?
You can specify the default Python language server by modifying VSCode’s setting item python.languageServer
, but of course in this article we should set it to Default
or Pylance
to ensure that the extension Pylance
is employed. To accomplish the above, you need to search for @id:python.languageServer
in the Settings Panel or edit the settings.json
file directly.
Pylance Usage and Setup Instructions
For a detailed look at the use and setup of the extension Pylance
, you can check out the article VSCode Pylance Extension Usage and Setup Instructions.
Install the VSCode autopep8, Black Formatter Extensions
The autopep8
(ms-python.autopep8) and Black Formatter
(ms-python.black-formatter) extensions provide formatting of Python code for VSCode.
How do I specify the default Python formatter for VSCode?
When you have both the autopep8
and Black Formatter
extensions enabled and have not yet specified a default formatter, VSCode will ask you at the appropriate time to make a choice, which can be modified either by searching for @lang:python editor.defaultFormatter
in the Settings Panel, or by editing the settings.json
file; in the case of editing settings.json
, the setting item editor.defaultFormatter
should be set to ms-python.autopep8
or ms-python.black-formatter
.
Install the VSCode Pylint, Flake8 Extensions
The Pylint
(ms-python.pylint) and Flake8
(ms-python.flake8) extensions provide error correction and quality checking of Python code for VSCode. They check the Python script files that have already been opened and send the results back to the developer.
Write Python Code in VSCode
After the relevant extensions are ready, you can open any folder as a workspace in VSCode and if asked Do you trust the authors of the files in this folder?, select Yes, I trust the authors.
In the Explorer Panel, create a new Python script file with the extension py
, such as hello_world.py
. Write code in that file that calls the print
function to display a welcome message, and the relevant extensions will respond by, for example, displaying instructions for using the print
function.
print('Hello, Python!')
Run Python Code in VSCode
VSCode provides us with several ways to run or debug Python code.
For example, clicking the Run/Debug button in the code window will run or debug the Python script file corresponding to the current window. Alternatively, open the Run and Debug Panel, click create a launch.json file and select Python Debugger | Python File (if necessary), which will add the configuration for running and debugging the current Python script file to the launch.json
file, at which point, press F5 to run and debug the currently active Python file.
If the launch.json
file does not exist or there is no associated configuration in that file, you will need to select one of the available configurations to start running or debugging after pressing F5.
Hello, Python!