[Django] how to use pre-commit

2021-03-13 hit count image

Let's see how to use pre-commit to execute the flake8 when you commit to Git.


On the previous blog post, I’ve introduced how to configure the static code analysis flake8 to make the same code style and reduce the potential bugs.

In this blog post, I will show you how to use pre-commit to execute the flake8 when you commit the source code to Git.

pre-commit installation

To execute the flake8 automatically by pre-commit, we need to install the pre-commit. Execute the command below to install the pre-commit.

pip install pre-commit

After installing, don’t forget to store it to requirements.txt.

pip freeze > requirements.txt

Done! we’ve seen how to install the pre-commit.

pre-commit configuration

To use the pre-commit to execute the flake8 automatically, we need to create the configuratoin file of the pre-commit. Execute the command below to create a configuration file of the pre-commit.

pre-commit sample-config > .pre-commit-config.yaml

When you open the created .pre-commit-config.yaml file, you can see the contents like below.

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v3.2.0
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml
      - id: check-added-large-files

We can see the sample of the configuration file provided by the pre-commit.

flake8 configuration

The pre-commit configuration file .pre-commit-config.yaml has the structure below basically.

  - repo: repo-url
    rev: version
      - id: hook-id
  • repo: Repository URL from the pre-commit
  • rev: Version of the feature
  • id: Feature from pre-commit

The pre-commit provides some features. You can see them on the link below.

We’ll use the flake8. Open the .pre-commit-config.yaml file and modify it like below.

  - repo: https://gitlab.com/pycqa/flake8
    rev: 3.8.4
      - id: flake8

I’ve configured the repo URL, version and flake8 hook of the pre-commit. You can see the configuration on the flake8 official documentation.

To apply the configurations to Git Commit hook, execute the command below.

pre-commit install

Done! we’ve ready to use the pre-commit to use the flake8

Execute pre-commit

Let’s check the configuration we’ve set is working well. Execute the command below, that is executed based on .pre-commit-config.yaml file, to start the pre-commit.

pre-commit run --all-files

You can see the result when you execute the command.


We’ve checked the pre-commit, that we’ve configured, is working well. Commit the configurations to Git.

git add .
git commit -m 'Add pre-commit for flake8'
git push origin main


We’ve seen how to configure the pre-commit to execute the flake8 when we commit to Git. Note that if you clone the repository, don’t forget to execute pre-commit install to register the pre-commit configuration to Git hooks.

git clone repository_url
# virtualenv venv
# source venv/bin/activate
pip install -r requirement.txt
pre-commit install

So let’s do more productive development with automated flake8!

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.