Dan Bader

How code linting will make you awesome at Python

In Python code reviews I’ve seen over and over that it can be tough for developers to format their Python code in a consistent way: extra whitespace, irregular indentation, and other “sloppiness” then often leads to actual bugs in the program.

Luckily automated tools can help with this common problem. Code linters make sure your Python code is always formatted consistently – and their benefits go way beyond that.

What code linters can do for you

A code linter is a program that analyses your source code for potential errors. The kinds of errors a linter can detect include:

  • syntax errors;
  • structural problems like the use of undefined variables;
  • best practice or code style guideline violations.

I find code linting to be an indispensable productivity tool for writing Python. It’s possible to integrate linting into your editing environment. This gives you immediate feedback on your code right when you type it:

For some classes of errors, linting can shorten the usual write code, run code, catch error, fix error loop to write code, see and fix error. This difference might not seem much – but in the course of a day these time savings add up quickly and can have a huge impact on your productivity.

In short, code linters are great!

Which Python linter should I use?

Python has several good options for code linters. The ones I’m listing here are available for free and are open-source software:

  • Flake8 is my personal favorite these days. It’s fast and has a low rate of false positives. Flake8 is actually a combination of several other tools, mainly the Pyflakes static analysis tool and the Pycodestyle (former pep8) code style checker.

  • Pylint is another good choice. It takes a little more effort to set up than Flake8 and also triggers more false positives. On the other hand it provides a more comprehensive analysis. Definitely not a bad choice – but I’d stick with Flake8 if you’re just starting out.

I’m sold – what’s the quickest way to get started?

If you’re not using a linter yet you’re missing out on some really great benefits. But don’t worry, I’ve got your back – I recorded a 5 minute Python linting video tutorial you can watch below.

In the video I’ll give you the run down on how to set up the Flake8 Python linter from scratch. With a few simple steps you’ll be able run a code linter on your own Python programs. I’ll also demonstrate how linter feedback can be integrated with your code editor (I’m using Sublime Text 3 in the video).

I’ve seen great results from using linters. I believe they’re one of the quickest ways to improve your Python skills. Spend 5 minutes to try out Flake8 – I’m sure it’ll be well worth your time 😊

Enjoy the video:

» Subscribe to the dbader.org YouTube Channel for more Python tutorials.

Your Shortcut to a Productive Python Setup with Sublime Text: Get a professional Sublime Text setup for writing Python that makes you more productive and will be an absolute joy to use. » Click here to learn more

5 Sublime Text Plugins to Boost Your Python Productivity

Free 5-day class—just enter your email address below:

This article was filed under: code-review, craftsmanship, programming, python, and sublimetext.

Related Articles:
Latest Articles:

Your Shortcut to a Productive Python Setup with Sublime Text: Get a professional Sublime Text setup for writing Python that makes you more productive and will be an absolute joy to use. » Click here to learn more

← Browse All Articles