如何使用带有 python 虚拟环境的本地 flake8 插件?

How to use a local flake8 plugin with a python virtual env?

我正在尝试将项目与 flake8 插件集成,我作为本地插件(例如不是 PyPI 包)编写,如 here 所述。该项目在本地和 github 工作流程中使用虚拟环境。由于 flake8 是从虚拟环境中调用的,因此它找不到插件,该插件作为项目根目录下的一个文件夹。当我手动将插件代码添加到虚拟 env 文件夹时,它很好地集成并且 flake8 能够找到并执行它。

该解决方案闻起来像某种 github pre-commit-config/hook,但我在文档中找不到此用例的任何参考。目前 flake8 在 pre-commit-config 中配置如下:

  repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.2.0
    hooks:
      - id: debug-statements
  - repo: https://github.com/PyCQA/flake8
    rev: 4.0.1
    hooks:
      - id: flake8
        additional_dependencies: ['dlint']

有没有办法在本地/github 工作流程中使用我的非打包 flake8 插件和虚拟环境?

您需要配置 paths 以确保 flake8 将适当的目录放在 sys.path 上以发现您的插件

您链接的文档中提到了这一点,再往下一点:

However, if you are working on a project that isn’t set up as an installable package, or Flake8 doesn’t run from the same virtualenv your code runs in, you may need to tell Flake8 where to import your local plugins from. You can do this via the paths option in the local-plugins section of your config:

[flake8:local-plugins]
extension =
  MC1 = myflake8plugin:MyChecker1
paths =
  ./path/to

请注意,如果您的本地插件具有所需的依赖项,则还需要在您的 pre-commit 配置

中的 additional_dependencies 中列出这些依赖项

免责声明:我是当前的 flake8 维护者,我创建了 pre-commit

原来 paths 属性 配置错误,应该是

paths =
  . flake8_plugins/

而不是

paths =
  .flake8_plugins/