Quick wins in improving your Python codebase health
Tags: python, django
Published: 2022-12-31 11:56:59

---
title: Quick wins in improving your Python codebase health
published: true
date: 2022-12-31 11:54:38 UTC
tags: Python,Django
canonical_url: https://www.rockandnull.com/python-code-formatter/
---

 ![Quick wins in improving your Python codebase health](https://images.unsplash.com/photo-1600658747056-eb00845297a5?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDF8fG9yZ2FuaXNlZHxlbnwwfHx8fDE2NzI0ODc0MjY&ixlib=rb-4.0.3&q=80&w=2000)

Keeping your codebase as "healthy" as possible is the common aim for most engineering teams out there. Even though Python uses indentation to indicate a block of code and it's by default more readable than other languages, in a complex project this is not enough.

There are countless ways to keep your Python as tidy and readable as possible. Here, I aim to cover the easiest yet most impactful ways to do that in your Python codebase.

## Sourcery: code refactoring suggestions

One of the first tools I install when setting up my Python dev environment is [Sourcery](https://sourcery.ai/). This still uses AI/ML to suggest code improvements to your Python code, but unlike [GitHub's Copilot](https://github.com/features/copilot), it won't write code for you.

The suggestions range from simplifying blocks of code to refactorings to make the code more "Pythonian". This is especially useful for people with a background in another programming language who tend to write Python with another's language conventions. Using this tool, it will be faster for a new-to-Python engineer to learn the Python way of doing things.

## Black: the ultimate code formatter

The ultimate linter and code style fixer for Python code is [Black](https://github.com/psf/black). All you need is to run `black .` in your root directory, and the entire codebase will magically be converted into using a consistent code style.

Black is highly opinionated ([The Black Code Style](https://black.readthedocs.io/en/stable/the_black_code_style/index.html)), but configurable if needed. I prefer not to configure the code style if possible since the aim is for the code formatter to produce consistent code no matter in which project is used. Ideally, as the project description mentions, is for the code style is to become so transparent that it will save time and mental energy to focus on the more important aspects of the code, such as business logic.

If you end up using this code formatter, I would highly recommend implementing a  deeper integration: include it in your CI/CD pipeline ([GitHub Actions](https://black.readthedocs.io/en/stable/integrations/github_actions.html) supported natively), or set up a [Git pre-commit hook](https://black.readthedocs.io/en/stable/integrations/source_version_control.html).

## Autoflake: remove unused imports

Having unused imports in a Python file is a prevalent issue, with a very easy solution: [autoflake](https://github.com/PyCQA/autoflake). Running it over your files will remove any unused imports in place.

In case you have an import that you want for its side effects and want it not to be removed by the tool, then append `# noqa` to the [relevant import line](https://stackoverflow.com/a/69135982/2456568).

Hopefully, with these few tools, you can quickly and easily improve your Python code base quality.

Happy coding!