Make your Python code look good with Black on Fedora

The Python programing language is often praised for its simple syntax. In fact the language recognizes that code is read much more often than it is written. Black is a tool that automatically formats your Python source code making it uniform and compliant to the PEP-8 style guide.

How to install Black on Fedora

Installing Black on Fedora is quite simple. Black is maintained in the official repositories.

$ sudo dnf install python3-black

Black is a command line tool and therefore it is run from the terminal.

$ black --help

Format your Python code with Black

Using Black to format a Python code base is straight forward.

$ black myfile.py
All done! ✨ 🍰 ✨ 1 file left unchanged.
$ black path_to_my_python_project/
All done! ✨ 🍰 ✨
165 files reformatted, 24 files left unchanged.

By default Black allows 88 characters per line, meaning that the code will be reformatted to fit within 88 characters per line. It is possible to change this to a custom value, for example :

$ black --line-length 100 my_python_file.py

This will set the line length to allow 100 characters.

Run Black as part of a CI pipeline

Black really shines when it is integrated with other tools, like a continuous integration pipeline.

The –check option allows to verify if any files need to be reformatted. This is useful to run as a CI test to ensure all your code is formatted in consistent manner.

$ black --check myfile.py
would reformat myfile.py
All done! 💥 💔 💥
1 file would be reformatted.

Integrate Black with your code editor

Running Black during the continuous integration tests is a great way to keep the code base correctly formatted. But developers really wants to forget about formatting and have the tool managing it for them.

Most of the popular code editors support Black. It allows developers to run the format tool every time a file is saved. The official documentation details the configuration needed for each editor.

Black is a must-have tool in the Python developer toolbox and is easily available on Fedora.

For Developers

12 Comments

  1. brokkr

    Sounds interesting. But say you have internalized PEP8 and is guided by feelings of shame and guilt not to deviate from it’s guidelines… does it add anything that you’re not doing yourself?

    • Noah

      Output appears to be different between black and autopep8, so probably not what you want.

  2. qoheniac

    The package name is python-black and not just black.

  3. Imtiaz A Khan

    python-black does not work either!

    My workstation is Fedora 30.

    • qoheniac

      Yeah, sorry! I meant python3-black like it’s now also in the article.

    • Charles

      Have a look at “dnf search python-black”. It will answer with:
      ========================= Name Matched: python
      -black ======================
      python3-black.noarch : The uncompromising code formatter

    • Charles

      Note that an asterisk (‘*’) between “python” and the dash (‘-‘) is needed (it was wiped out after I had entered my previous comment).

  4. Lizard_Ohama

    Black also can use on vim with plugin ALE or plugin Syntastic.

  5. I love it so much! tears!

  6. Robert Beckwith

    Interesting!

  7. Nice article. thanks for sharing.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

The opinions expressed on this website are those of each author, not of the author's employer or of Red Hat. Fedora Magazine aspires to publish all content under a Creative Commons license but may not be able to do so in all cases. You are responsible for ensuring that you have the necessary permission to reuse any work on this site. The Fedora logo is a trademark of Red Hat, Inc. Terms and Conditions

%d bloggers like this: