4 Python Packages to Beautify and Format Your Codebases

Automate clean up of your Python code

Tapas Das
Better Programming

--

Photo by Andrew Neel on Unsplash

Have you ever come across a poorly written piece of Python code?

I’m talking about a tangled mess where you had to spend hours just trying to understand what piece of code goes where.

Writing code is one part of a developer’s role. Writing beautiful and neat Python code, on the other hand, is a different ball game altogether. This could well make or break your image as a proficient programmer in the analytics or data science space (or even in software development).

So how do we write this so-called beautiful Python code?

PEP 8, sometimes spelled PEP8 or PEP-8, is a document written in 2001 by Guido van Rossum, Barry Warsaw, and Nick Coghlan, that provides guidelines and best practices on how to write Python code. The primary focus of PEP 8 is to improve the readability and consistency of Python code.

As Guido van Rossum said, “Code is read much more often than it is written.” You may spend a few minutes, or a whole day, writing a piece of code to process user authentication. Once you’ve written it, you’re never going to write it again. But you’ll definitely have to read it again. That piece of code might remain part of a project you’re working on. Every time you go back to that file, you’ll have to remember what that code does and why you wrote it, so readability matters.

If you’re interested, PEP 8 can be found here.

Motivation

When writing Python code, as developers, you need to make sure that the code:

  • looks nice
  • is organized
  • conforms to the PEP 8 style guide
  • includes docstrings

However, it can be overwhelming to check all of these criteria manually, every time you write a piece of code. Wouldn’t it be nice if you can automatically check and format your code before committing to a version control system?

Let’s get started.

A Sample Garbage Python Code

Let’s start by writing a messed-up Python code that will violate all PEP 8 standards.

1. flake8

We will start with checking the style and quality of Python code, by using the flake8 Python package.

To install flake8, type:

pip install flake8

Below is the output of running flake8 on our Python code.

As we can see, flake8 has pointed out below error types.

  1. Imported but unused libraries
  2. Indentation and line too long errors, violating PEP 8 style guide

2. black

Now that we know what the errors are, let’s start by formatting the code with a black Python package.

To install black, type:

pip install black

Below is the output of running black on our Python code.

And voila! The code is automatically formatted like below!

3. isort

As the next step, let's try to sort the imported libraries alphabetically and separate them into sections and types, for more organized code.

To install isort, type:

pip install isort

Below is the output of running isort on our Python code.

Cool! The imports are much more organized now.

4. interrogate

Sometimes, we might forget to write docstrings for classes and functions like below:

So as a final step, instead of manually looking at all our functions and classes for missing docstrings, we can run interrogate instead to check for missing docstrings.

To install interrogate, type:

pip install interrogate

Below is the output of running interrogate on our Python code.

Voila! From the terminal output, we know which files, classes, and functions don’t have docstrings.

Since we know the locations of missing docstrings, adding them is easy.

Conclusion

Congratulations! You have just learned how to automatically check, edit and beautify your Python code before committing it. I hope this blog will make it effortless for you to review and format your code.

--

--