如何编写 pep8 配置 (pep8.rc) 文件?

How to write a pep8 configuration (pep8.rc) file?

我找到了 pep8 的文档,但无法理解如何编写这些文档。除了设置 max-line-length 和 ignore 之外,我什至找不到任何带有选项的示例。

我正在尝试编写一个 .pep8.rc 文件,除其他外,我需要执行以下操作:

有人可以回答一个例子或 link 一个吗?

首选方法是在项目的顶层使用setup.cfg(.cfg 与.ini file 具有相同的语法),其中应包含[pep8]部分。例如:

[pep8]
ignore = E226,E302,E41
max-line-length = 160

注意:错误代码在pep8 docs.

中定义
  • autopep8 找到与 pep8 相同的 [pep8] 部分。
  • flake8 在 setup.cfg.
  • 中需要一个 [flake8] section
  • yapf 在 setup.cfg.
  • 中寻找 [yapf] section

遗憾的是,Andy Hayden 的回答不适用于 pytest / pytest-pep8 / flake8.

pytest-pep8

为此,您必须使用其中之一

# content of setup.cfg
[pytest]
pep8maxlinelength = 99

[pytest]
max-line-length=99

奇怪的是,以下不起作用

[tool:pytest]
max-line-length=99

pytest-flake8

添加

 [flake8]
 max-line-length=99

他们将 pep8 重命名为 pycodestyle 以避免混淆。

您可以创建一个 setup.cfg 文件:

[pycodestyle]
ignore = E226,E302,E41
max-line-length = 119
exclude =
    tests/
    docs/

有关错误代码,您可以阅读 this 文档。