Commit 前检查 Python 代码格式.md

https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/

  • 代码检查工具:flake8
  • 借助钩子(hooks): 在特定的重要动作发生时触发自定义脚本
  • pre-commit 钩子:在 pre-commit 前操作

步骤

  1. 安装 pre-commit 包,用于使用 pre-commit 钩子
    • pip install pre-commit
  2. 工程目录下添加 .pre-commit-config.yaml 设置需要设置的钩子
  3. ~/.flake8 设置 flake8 规则
  4. pre-commit install 安装钩子到 git
  • .pre-commit-config.yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v1.2.3
hooks:
- id: flake8
  • ~/.flake8
[flake8]
ignore = E501 E722 W291
max-line-length = 120

使用 pyenv 遇到的错误

(ENV37) [18:59:05] ~/Workspace/medweb git(feature/seo_auto_audio_push) 🔥 ❱❱❱ gcm 'tmp'
pyenv: python3.7: command not found

The `python3.7' command exists in these Python versions:
3.7.0
3.7.0/envs/ENV-DEMO-FFmpeg
3.7.0/envs/example_venv
3.7.0/envs/medweb
ENV-DEMO-FFmpeg
example_venv
medweb

Note: See 'pyenv help global' for tips on allowing both
python2 and python3 to be found.
[18:59:06] [cost 0.293s] gcm 'tmp'

解决方式

cd /Users/deppwang/.pyenv/versions/ENV37/bin/
ln -s python3 python3.7

因为新建了一个 .pre-commit-config.yaml 文件,又不能提交,可以 关闭 GIT 跟踪这个文件修改。或加到 .gitignore 中,关闭对 .gitignore 的跟踪

git update-index --skip-worktree /Users/deppwang/workspace/medweb/.pre-commit-config.yaml

git update-index --skip-worktree /Users/deppwang/workspace/medweb/.gitignore
Depp Wang wechat
个人公众号