Automate Python Workflow With Pre-Commits: Autopep8 and Pylint

by Edgar Ong | Last Updated: May 5, 2021

Collaborating on a Python project? Keep the code base unified between contributers using pre-commit to streamline the workflow.

What is pre-commit?

Pre-commit is a Git hook that triggers when git commit is attempted.

For Python projects, I use it to spend less time formatting my code and more time working on features and higher priority tasks.

Get started

  • pip install pre-commit
  • add config file (.pre-commit-config.yaml), here's an example:
    - repo:
      rev: pylint-2.7.2  # Use the sha / tag you want to point at
      - id: pylint
        exclude: ^server/
    - repo:
      rev: v1.5.4  # Use the sha / tag you want to point at
      - id: autopep8
        exclude: ^server/
  • Run pre-commit install
  • Run pre-commit run --all-files the first time pre-commit is added to the project so all files are checked

Moving forward, pre-commit is triggered when git commit is attempted.


Commits are not saved until they pass the pre-commit.

Based on the example configuration, all files must pass pylint then the same files are formatted to PEP 8 standard using autopep8.

Errors will provide details about the files and why the process failed. Fix the issues and commit again until they pass.

Pre-commit prevents pushing to the code base until they pass. Can you see the value in this? :)