The Ultimate VS Code Setup for Python Developers: Tips, Tricks, and Essential Extensions
As a Python developer, your choice of Integrated Development Environment (IDE) can make a huge difference in your productivity, code quality, and overall programming experience.
In this article, we’ll explore some of the best VS Code setups and configurations for Python development, including essential extensions, customization, and tips that can help you take your Python coding to the next level.
Extensions
Python: with the Python Extension, you can take advantage of features like code highlighting, IntelliSense, code navigation, and code formatting. Additionally, it includes built-in support for popular Python frameworks like Django, Flask, and Pyramid. The extension also provides an interactive Python interpreter, enabling you to experiment with code snippets and perform quick calculations without leaving the editor. With integrated debugging support, you can quickly identify and fix any issues in your Python code.
Python Indent: the Python Indent extension helps to automate the process of indentation, adjusting the code blocks as you type. With this extension installed, you can focus on writing your code, and the extension will take care of the indentation for you. This can save a considerable amount of time and reduce errors caused by incorrect indentation. The Python Indent extension also supports multiple indentation styles, including tabs, spaces, and mixed, allowing you to customize the indentation style to match your coding preferences.
autoDocstring — Python Docstring Generator: Docstrings are essential elements of Python code, as they provide documentation for functions and methods, explaining what they do and how they work. However, writing docstrings can be a time-consuming and tedious process, especially when you have to write them for numerous functions and methods. The autoDocstring extension helps to automate this process, generating docstrings automatically based on the function or method signature. With just a few keystrokes, you can generate a complete docstring that includes information on the function’s parameters, return values, and any exceptions that may be raised. The autoDocstring extension also supports customization, allowing you to define your docstring format and style.
Configurations
Once you have installed the previous extensions, it is time to edit the settings of Visual Studio Code. This IDE has two settings file, the defaultSettings.json
which has the default configuration of the IDE and the settings.json
which is the user configuration. The latter is the one that we are going to modify with custom parameters.
First of all let’s see how to find this file. You can directly navigate to the settings.json
file on your computer by following these steps:
Open Visual Studio Code.
Open the Command Palette by pressing “Ctrl + Shift + P” (Windows and Linux) or “Cmd + Shift + P” (macOS).
In the Command Palette, type “Open User Settings” and select “Preferences: Open User Settings” from the list.
In the Settings editor, click on the “Open Settings (JSON)” link at the top right corner of the window.
This will open the settings.json
file in the editor, where you can edit the settings for Visual Studio Code. The first time it looks like very minimal with just few parameters like this:
{
"workbench.colorTheme": "SynthWave '84",
"window.zoomLevel": 2,
}
In my case the colorTheme
(I love SynthWave ’84) and the zoomLevel
of the IDE window. These are general settings, not related to a language, to define some specific parameters just on a specific file format you need something like this:
{
"workbench.colorTheme": "SynthWave '84",
"window.zoomLevel": 2,
"[python]": { // python section
"editor.rulers": [88],
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"editor.formatOnType": true
},
}
Introducing the "[python]"
key you can add a configuration, which will be applied just when you are editing a .py
file. As you can see these are editor’s configurations, like editor.rulers
which insert a vertical line after 88 characters.
I highly suggest to have a section for each language, in this way you have always the best experience without any overlapping setting.
Let’s move on the section regarding python’s extensions settings.
{
"workbench.colorTheme": "SynthWave '84",
"window.zoomLevel": 2,
"[python]": { // python section
"editor.rulers": [88],
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"editor.formatOnType": true
},
// Local interpreter can be the default python
// or if you are using poetry "$(poetry run which python)"
"python.defaultInterpreterPath": "/usr/local/bin/python3",
"python.linting.enabled": true,
"python.formatting.provider": "black",
"python.sortImports.args": ["--profile", "black"],
"python.analysis.typeCheckingMode": "strict",
"python.analysis.autoSearchPaths": true,
"python.languageServer": "Default",
"python.linting.pylintEnabled": true,
"python.linting.flake8Enabled": true,
"python.linting.flake8Args": [
// Match what black does.
"--max-line-length=88"
],
}
The above settings.json
file contains Python-related settings for Visual Studio Code. Here is a breakdown of each setting:
python.defaultInterpreterPath
: Specifies the path to the default Python interpreter. In this case, it is set to/usr/local/bin/python3
.python.linting.enabled
: Determines whether linting is enabled for Python code. It is set totrue
here, meaning that linting is disabled.python.formatting.provider
: Specifies the formatting provider to use for Python code. Here, it is set to use theblack
formatter.python.sortImports.args
: Specifies the arguments to use when running theisort
utility for sorting imports in Python code. Here, it is set to--profile black
, meaning that theblack
profile is used for sorting.python.analysis.typeCheckingMode
: Specifies the type checking mode to use for Python code. Here, it is set tostrict
.python.analysis.autoSearchPaths
: Determines whether Visual Studio Code should automatically search for Python packages installed in the project's environment. It is set totrue
here.python.languageServer
: Specifies the language server to use for Python code. Here, it is set toDefault
, meaning that the default language server is used, alternative could bePylance
python.linting.pylintEnabled
: Determines whetherpylint
is enabled for linting Python code. It is set tofalse
here.python.linting.flake8Enabled
: Determines whetherflake8
is enabled for linting Python code. It is set totrue
here.python.linting.flake8Args
: Specifies the arguments to use when runningflake8
. Here, it is set to use a--max-line-length
argument with a value of88
, which matches what theblack
formatter does.
Command Shortcuts
Bonus section regarding the command shortcuts, which is not strictly related to python but a recap is always useful.
Note: Some shortcuts may differ depending on your operating system and keyboard layout.
That’s a wrap! Thanks for reading, I hope you will find it interesting a useful. Let me know if you have suggestions for other useful configurations to consider!