How to Automate Code Formatting in Python - ByteScout
  • Home
  • /
  • Blog
  • /
  • How to Automate Code Formatting in Python

How to Automate Code Formatting in Python

A good Python programmer not only writes code that works but also ensures that the code is readable and easy to understand. Without proper formatting, it can get really difficult to understand how the program works, which in turn makes it more difficult debugging the code or when you (or another contributor) are trying to improve it.

But with that being said, formatting code needs a lot of time investment. Even if you’re working in an IDE or using tools like Pylint or Flake8, it will only tell you if there are any errors in the code or syntax, but won’t fix them. You have to fix it yourself.

This is where code formatting programs come into the picture. Using these tools, you can automatically make your code aesthetically clean and improve its readability. And so, for this tutorial, we present you with a guide on how to automate code formatting in Python.

  1. Tools for Automating Code Formatting in Python
  2. How to Automate Code Formatting in Python using Black

Automate Desktop Applications

Tools for Automating Code Formatting in Python

Most code formatting automation tools for Python follow PEP 8 – Python’s official style guide. The tools will first check the code and see if it follows the formatting style recommended in PEP 8. If not, then it will automatically implement the styles.

Now there are tons of tools that can help you with this task. Here is a look at some of the most popular and widely used Python Code Formatters.

autopep8

autopep8 is an extremely popular, yet unofficial code formatter for Python. It is based on pycodestyle – Python’s official PEP 8 violation checker.

YAPF

Even if your Python code follows the PEP 8 guidelines, that doesn’t ensure that it looks clean and aesthetically pleasing. This is where YAPF, short for Yet Another Python Formatter comes in.
Its functionality is similar to other automatic code formatting tools like clang-format or gofmt.

Black

Black is by far the most popular and currently considered as the defacto code formatter for Python. In fact, it is used by some of the most popular open-source projects like Pytest, Pyramid, Poetry, and many more.
If you have Black installed, you can just forget about styling your code and concentrate on writing it. Then once you’re done, just run Black and it will make it aesthetically beautiful in a matter of seconds.

For this guide, we will be showing you how to use Black to automatically format the Python code.

How to Automate Code Formatting in Python using Black

Before you can use Black to reformat your Python code, you will first need to install it. Thankfully it’s a hassle-free process. All you need to do is enter the following command in your command line and it’s installed – pip install black.

Once you have Black installed, it will become available as a new command-line tool. Let’s now see how we can use it to automatically format our Python code.

    1. How to Format a Single File using Black
    2. How to Format Multiple Files using Black
    3. How to Check Files for Correct Formatting using Black
    4. Black Customizations: Changing the Number of Characters per Line

How to Format a Single File using Black

First, let’s see how to use Black to format a single file.

For this tutorial, we will be using the file sum_of_first_100_numbers.py. Here is the unformatted code:

Now to format it using Black, we will open the terminal (in our case we are using the Windows Powershell) and then enter black {source_of_the_file}.

This is how it should look in the terminal:

Now let’s check out the formatted code:

As you can see, after using Black, the code now has proper spacing, and it has also changed the single quotation used to double quotation.

It might not be too evident as it’s a small file, but imagine a large program with over 10,000 lines of code including modules, imports, etc. In that case, Black will format the code to properly arrange everything and make the code much more readable, and that too, automatically.

How to Format Multiple Files using Black

You can tweak the command black {source_of_the_file} to black {source_of_the_folder}. By doing this, Black will automatically format every single Python file inside that folder.

It is worth noting that you can’t choose to specify a set of particular Python files to format them with Black. You can either just format a single file or an entire folder.

As such, if you wish to only format a few select files, then a useful trick is to just create a new folder containing the files and then use Black to format them.

How to Check Files for Correct Formatting using Black

Sometimes, you might not want Black to make changes to a file but still want to know whether the formatting is readable or not. In that case, you can use the command: black –check. and It will check the current folder and show you which Python files can be formatted, but don’t format the files.

Here is a look at how it works:

Note: Don’t leave out the period(.) at the end of the command. It’s not a typo.

As you can see, Black recommends that two of the files be formatted.

Now, to know what formatting it recommends without applying the formatting on a given file, you can enter the following command: black –check –diff file_name.py and it will give you an output as given below.

As you can see, the tool will directly show you in the shell what changes it will make to the selected file. You can now go over these changes before deciding whether or not you want to reformat the file or not.

Black Customizations: Changing the Number of Characters per Line

By default, Black only allows a maximum of 88 characters as the line length. However, you can easily change that by formatting a file or folder using the “-l” option or “–line-length” option.

For example, if we want to format the file First_Program.py using Black and only want a maximum of 20 characters per line, then we can use the following command: black -l 20 First_Program.py.

   

About the Author

ByteScout Team ByteScout Team of Writers ByteScout has a team of professional writers proficient in different technical topics. We select the best writers to cover interesting and trending topics for our readers. We love developers and we hope our articles help you learn about programming and programmers.  
prev
next