If you want to automatically format your code, check if your code’s compliance with PEP8, and sort your imports before committing your files, use pre-commit hooks.
If all files are in the right format, the commit will be made. Otherwise, the commit will be denied.
I use pre-commit hooks for most of my projects to maintain code quality standards.
Here is the link to the article I found that shows you how to use pre-commit with black, flake8, and isort.