Skip to content

Linting Python in Visual Studio Code

Published: | 4 min read

Table of contents

Open Table of contents

Intro

A little over a year ago I wrote how to lint Python right within Sublime Text, Three steps to lint Python 3.6 in Sublime Text.

Since then, I have migrated a lot of my day-to-day work over to Visual Studio Code. Let’s have a look at my linting setup for this editor.

Prerequisites

Note: I run Zsh as my shell and Oh-My-Zsh on top of that. Bash will work too.

Prepare a Python project

First of all: I use virtualenv for all of my development projects, and so should you. I have no globally installed pip packages, all installations are per project within a virtualenv.

For the demo purposes, we will create a tiny project, which will contain some badly formatted code.

The easiest way to setup a project is to use a handy tool, such as my creation jetzt. All we have to do, is run

# Within our projects home directory
jetzt

… and follow the prompt.

Now we should be within our project directory, virtualenv activated.

Let’s create a well malformed Python file called app.py, with the content:

'''
Linting test.
'''

name = 'John Doe'

# This line contains a unecessarily long comment, which will test if my ignores are not ignored. Should raise no issue.

print(f'Hello {name}!')

def blah(addition=None):
    return f'Blah {addition}'

age = 30
my_string = f'He said his name is {name} and he is {age} years old.'

print(my_string)  #random comment

name != unknown  # this is wrong

my_list = [0,2,'foo', 'bar']



print(blah('blah'))

Install linter, Flake8

Right. Now for the linting, I like to use Flake8. Let’s install that, by running:

pip install flake8

We could lint our little problematic app.py right now, but let’s set some defaults for Flake8 first. As I stated above, I don’t like to install packages on global level. Yet I don’t like to maintain settings on project level, if they apply on global level. So, as stated in Flake8 docs, Flake8 looks global settings in the file ~/.config/flake8 on macOS and Linux.

Without further ado, let’s create that file if it does not exist, and fill in the content:

# If the file does not exist
touch ~/.config/flake8

# File content
[flake8]
ignore = E501
exclude =
    # No need to traverse our git directory
    .git,
    venv
max-complexity = 10

Little explanation:

Lint on terminal

Time for a test run. Let’s finally lint our test subject, by running:

flake8 app.py

We should get the following errors / warnings:

app.py:11:1: E302 expected 2 blank lines, found 1
app.py:14:1: E305 expected 2 blank lines after class or function definition, found 1
app.py:17:19: E262 inline comment should start with '# '
app.py:19:9: F821 undefined name 'unknown'
app.py:21:13: E231 missing whitespace after ','
app.py:21:15: E231 missing whitespace after ','
app.py:25:1: E303 too many blank lines (3)
app.py:25:20: W292 no newline at end of file

Not good for our code quality, good for our linter setup.

Setup Visual Studio Code

Now that we have our linter working on the terminal, it’s time to setup linting in VSCode. Did I forget to mention, that Visual Studio Code utilized all of that we did and set up above, so that was not just an academic exercise. You see, we are steadily moving towards our goal, step by step.

Step 1

Install Python extension for Visual Studio Code if you haven’t already. This gives us basic syntax highlight, code completion, debugging, and whatnot. Restart VSCode, when prompted.

Step 2

Open our project file, app.py. You might see VSCode telling, that we have not pylint installed. we can safely ignore that for now.

Step 3

Open VSCode settings, and add the following to the user settings:

"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
"python.linting.lintOnSave": true

Restart Visual Studio Code.

Step 4

Now we are all set up, and should see the same errors / warnings reported to us on the PROBLEMS tab. Screenshot of this can be seen on the article picture.

Add your favourite linting rules to the settings file, and start coding.

Further reading