Linting Python in VS Code [Video]
Tags: python, vscode, hintingandlinting
Published: 2023-04-11 23:50:18

---
title: Linting Python in VS Code [Video]
published: true
description: Power user settings when enabling your Python linter in VS Code. The goal is for your developer experience to be intuitive, replicable and shareable.
tags: #Python #VSCode #HintingAndLinting
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9oqw4z8po7sm87wo60xa.png
# Use a ratio of 100:42 for best results.
# published_at: 2023-04-11 16:00 +0000
---

{% embed https://www.youtube.com/embed/dF1wWo2aJCo?si=qtmSWvrsDYUbcRRk %}

One of the most important parts of writing code is making sure your code is readable. There are so many positive downstream effects of clean code from its ease to maintain and add features, debug subtle programming errors or find uninitialized variables. Some people call this code hygiene. VS Code has linter extension support that enables you develop faster, produce cleaner code and is tweaked to your set up.

## How VS Code handles Python linters

The linter development experience can live entirely within VS Code if you'd like. This means you do not need to `pip install pylint` for example. However in most collaborative programming projects, and old habits die hard, I install my linter in my virtual environment so if I want to use local terminal features of Pylint, I can.

> **🔥PRO-TIP: Set your default importStrategy**
> importStrategy is a field on any of the Python linter extensions for automatically setting the linter version in your IDE. I like to set mine to fromEnvironment in my User level settings so that it automatically checks the version in any project I'm working on with a team, but also allow VS Code to default to any Workspace level settings my team is sharing.  
> `settings.json`
> ```json
> // use the pylint version with extension
> "pylint.importStrategy": "useBundled"
>
> // use the pylint version found in requirements
> "pylint.importStrategy": "fromEnvironment"
>```

When you start your project, the first thing you will likely do is activate your virtual environment or open up a container to develop in (like [Dev Containers](https://code.visualstudio.com/docs/devcontainers/containers)). Linters are the development tool that is used to make sure your code is formatted consistently across your team. Then add your linter to your `requirements-dev.txt`-- or otherwise named file to store your development only requirements -- and `pip install -r requirements-dev.txt`. In VS Code, you'll select your Python interpreter by using shortcut key `Ctrl+P` to open up the Command Pallette, and select from the dropdown menu which environment you'd like to use for your project. I select the interpreter associated with my project environment.

![Screen shot of the Command Pallette dropdown menu. The top of the menu allows the user to search for a name. The first selection is the option to add an interpreter path. The following options are different Python versions VS Code has found on my machine. The last of five options is starred and "recommended" as the Python interpreter connected to my Python virtual environement.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/chtgt26jc8fu2ygojl8i.png)

There are many packages for code quality. At the time of this post, VS Code and its active extension-contributing community supports [flake8](https://marketplace.visualstudio.com/items?itemName=ms-python.flake8), [ruff](https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff), [pylint](https://marketplace.visualstudio.com/items?itemName=ms-python.pylint) and the newly released [mypy](https://marketplace.visualstudio.com/items?itemName=ms-python.mypy-type-checker). The Ruff linter was created from the [Python Extension template](https://github.com/microsoft/vscode-python-tools-extension-template) by [Charlie R. Marsh](https://github.com/charliermarsh/ruff-vscode). The VS Code team encourages community contributed packages and you can [learn more in the VS Code documentation](https://code.visualstudio.com/api/advanced-topics/python-extension-template). 

> **🔥PRO-TIP: VS Code extension works as soon as its installed**
> VS Code linting is automatically enabled once the extension is installed. You no longer need `python.linting.enabled` set to true under `settings.json`.

## Enable what you want and disable what you don't

Navigate to the activity bar to the far left and search for "Pylint." I often like to enable pre-release so I can get the latest features and report bugs if I come across them, doing my part for the community. 

*GIF: Enable Pylint*
![Enabling Pylint from the extensions panel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kr9btosl7hpynlwg237l.gif)

> **🔥PRO-TIP: Install isort**
> Install an import sorting extension like [isort](https://marketplace.visualstudio.com/items?itemName=ms-python.isort) then use the shortcut key `Shift+Alt+O`in order to sort your imports quickly.

> **🔥PRO-TIP: Toggle Problems tab**
> Use `Ctrl+Shift+M` to toggle open and close the Problems tab in VS Code.

*GIF: Solving my first problems in VS Code for a [Wagtail](https://docs.wagtail.org/en/stable/) project*
![Open problems tab in VS Code and sort import order of code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u25zdz6pdyii0bbsjuk0.gif)

You can specify problems to consistently ignore in your projects by adding disable flags to your settings. You can do this either in your settings panel `Ctrl+,` or with your `settings.json` `Ctrl+P` then typing "Settings JSON" in the text bar.  

Here's what it looks like to disable a few doc string problems among other arguments in Pylint. "Args" are always lists in brackets:
`settings.json`
```json
"pylint.arg": [
    "--reports",
    "12",
    "--disable=C0114",
    "--disable=C0115",
    "--disable=C0116",
]
```
You can also add these same arguments via your User or Workspace settings panel. The same settings work for other linter extensions:
`settings.json`
```json
"flake8.arg": ["--ignore=E24,W504", "--verbose"],
"ruff.args": ["--config=/path/to/pyproject.toml"]
```

![Add arguments to specify pylint settings in both the settings panel and settings.json](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s8lw017uvaprgdnbgc3f.gif)

> **🔥PRO-TIP: Enable lintOnSave**
> When enabling `lintOnSave`, you might also want to enable generic `files.autoSave` option. The combination provides frequent linting feedback in your code as you type.

Want to dig more into the VS Code Linting documentation?  [https://code.visualstudio.com/docs/python/linting](https://code.visualstudio.com/docs/python/linting)

### Keep in touch! 
I host The Python Pulse every second Friday of the month 11 AM PT / 2 PM ET / 7 PM UTC https://aka.ms/python-pulse-live

![Python Pulse thumbnail - Getting the most out of Python with VS Code and Azure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/airhgo6uiosb7em5pned.png)

... or join me on the [Microsoft Python Discord](https://aka.ms/python-discord)

### More Reading...
- VS Code Shortcuts [Windows](https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf) | [Linux](https://code.visualstudio.com/shortcuts/keyboard-shortcuts-linux.pdf) | [MacOS](https://code.visualstudio.com/shortcuts/keyboard-shortcuts-MacOS.pdf)
- [User and Workspace settings](https://code.visualstudio.com/docs/getstarted/settings)
- [Python Pulse Playlist](https://youtube.com/playlist?list=PLj6YeMhvp2S4aIxuGH0NaGXQZlVUBsH3E)
- [PyDay @ Microsoft - May 2](https://aka.ms/PyDay)