从Python运行PEP8检查

7

我无法从Python脚本中运行PEP8检查。

我不想显式地运行pep8.exe,因为我想自动化这些检查,而且在不同的平台上,pep8可执行文件可能位于不同的位置。

1个回答

7

PEP8高级用法介绍了如何在Python脚本中使用pep8。

引用一个例子:

import unittest
import pep8


class TestCodeFormat(unittest.TestCase):

    def test_pep8_conformance(self):
        """Test that we conform to PEP8."""
        pep8style = pep8.StyleGuide(quiet=True)
        result = pep8style.check_files(['file1.py', 'file2.py'])
        self.assertEqual(result.total_errors, 0,
                         "Found code style errors (and warnings).")

1
我已经看到了。但我仍然有问题。 1)如何打印所有发现的违规行为? 2)如何检查目录中的所有Python文件? - Felix
  1. 移除 quiet=True。
  2. 使用 os.walk 构建目录中所有Python文件的列表,并将其传递到 check_files()。
- demented hedgehog

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接