In this article, we’ll show you how to set up linting with popular Python linters like Flake8 and Pylint in Visual Studio Code using extensions.
First, let’s install the necessary extensions for our chosen linters: Flake8 and Pylint. To do so, open your VS Code settings by pressing Ctrl + , (Windows/Linux) or Command + , (Mac). Then, search for “extensions” in the search bar at the top of the window.
Next, click on “Extensions” from the left-hand menu and then type “Flake8” into the search bar. Click on the Flake8 extension by Don Jayamanne ShenukA and then click on “Install”. Repeat this process for Pylint as well.
Once you’ve installed both extensions, restart VS Code to ensure that they are properly loaded. Now let’s configure them!
To do so, open your settings by pressing Ctrl + , (Windows/Linux) or Command + , (Mac). Then, search for “flake8” in the search bar at the top of the window. Click on “Flake8: Linting” from the list of results and then click on “Edit in settings.json”.
In the editor that opens up, add the following configuration options to your settings.json file:
{
// Flake8 Configuration Options
"flake8.max-line-length": 100, // sets the maximum allowed line length to 100 characters
"flake8.ignore": [
"E265", // disables flake8's E265 (line too long) check for this file
"F403" // disables flake8's F403 (imported-module-with-executable-scripts) check for this file
],
"flake8.max-complexity": 12, // sets the maximum allowed complexity to 12
"flake8.per-file-ignores": [
{ "path": ["tests/*", "examples/*"] }, // ignores flake8 checks in tests and examples directories
{ "src/my_module.py": [] } // disables all flake8 checks for this specific file
]
}
These configuration options will set the maximum line length to 100 characters, disable certain Flake8 checks (E265 and F403), limit complexity to a maximum of 12, and ignore Flake8 checks in tests and examples directories. You can customize these settings based on your personal preferences or project requirements.
Next, let’s configure Pylint using the same method: open your VS Code settings by pressing Ctrl + , (Windows/Linux) or Command + , (Mac), search for “pylint” in the search bar at the top of the window, and then click on “Pylint: Linter”.
In the editor that opens up, add the following configuration options to your settings.json file:
{
// Pylint Configuration Options
"pylint.max-line-length": 100, // sets the maximum allowed line length to 100 characters
"pylint.ignore": [
"C0302", // disables pylint's C0302 (line too long) check for this file
"W0614" // disables pylint's W0614 (deprecated import) check for this file
],
"pylint.max-args": 5, // sets the maximum number of arguments allowed in a function to 5
"pylint.repeated-keyword": "error", // sets pylint to flag repeated keywords as an error
"pylint.disable": ["C0321", "W0703"] // disables pylint's C0321 (line too long) and W0703 (duplicate argument name) checks for this file
}
These configuration options will set the maximum line length to 100 characters, disable certain Pylint checks (C0302 and W0614), limit function arguments to a maximum of 5, enforce strict keyword usage with “error”, and disable pylint’s C0321 and W0703 checks for this file.