DEV Community

Cover image for How to setup Black and pre-commit in python for auto text-formatting on commit
Emmanuel (Emmo00)
Emmanuel (Emmo00)

Posted on

How to setup Black and pre-commit in python for auto text-formatting on commit


Today we are going to look at how to setup Black (a python code formatter) and pre-commit (a package for handling git hooks in python) to automatically format you code on commit.

All the Code used in this article can be found on this GitHub repo here


Setting up your project with this (automatic code formatting on every commit) can be very useful and helpful especially when setting up a GitHub repository you expect other contributors. This enforces coding formatting and fosters consistency in coding style.

Let's get right to it

  • Installing Black and pre-commit

First of all, we need to install these packages to our projects. You can use the following command to install them:

pip install black pre-commit
Enter fullscreen mode Exit fullscreen mode

You might also need to add these packages to your requirements.txt file so other contributors can install the need packages by running pip install -r requirements.txt. You can do this by running pip freeze, and the coping the output to your requirements.txt file.

  • Configure pre-commit

The next step is configuring pre-commit. To do this, create a file called .pre-commit-config.yaml:

touch .pre-commit-config.yaml
Enter fullscreen mode Exit fullscreen mode

Then copy the following configurations to this file.

  - repo:
    rev: 22.3.0
      - id: black
        args: [--line-length=88]
        language_version: python3.11
Enter fullscreen mode Exit fullscreen mode

This tells Pre-Commit to use Black version 22.3.0 and format all Python files with a line length of 88 characters (you can adjust this to your preference).

  • Run pre-commit

Now we have our configurations ready, make sure that the current folder you are in is a git/github project (With the .git folder)
Now, run the following command:

pre-commit install
Enter fullscreen mode Exit fullscreen mode

This installs pre-commit hooks to .git\hooks\pre-commit.

Note: you need an internet commit for the first commit you make. This is to enable pre-commit initialize the environment for black, but once the environment is setup, it'd be reused.

$ git commit -m "add pre-commit configuration"
[INFO] Initializing environment for      
[INFO] Installing environment for        
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
black................................................(no files to check)Skipped
[main 6e21eab] add pre-commit configuration
 1 file changed, 7 insertions(+)
Enter fullscreen mode Exit fullscreen mode

That's it, you are ready to use the automatic code formatter. Just create a python file, run git add ., and then run git commit -m "Commit message". You'd notice that black attempts to format the file you just created. If you get the following message:

- hook id: black
- files were modified by this hook


All done! \u2728 \U0001f370 \u2728
1 file reformatted.
Enter fullscreen mode Exit fullscreen mode

Just run git add and git commit again.

Enjoy autoformatted code from now on😅. Bye👋

Top comments (0)