预提交 isort 没有名为 'setuptools' 的模块

pre-commit isort no module named 'setuptools'

我正在尝试 运行 预提交挂钩,但是当它们遇到 isort 挂钩时失败,抛出以下错误:

  File "/home/el/.cache/pre-commit/repoffrjhcx0/py_env-python3/lib/python3.10/site-packages/_distutils_hack/__init__.py", line 92, in create_module
    return importlib.import_module('setuptools._distutils')
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named 'setuptools'

我正在使用 docker,我已经检查过我的计算机和 docker 上都安装了 setuptools。我不明白为什么会出现此错误。我认为 isort 设置了它自己的环境,但是为什么不安装它,因为它在它们的配置文件中定义 pyproject.toml.

以下是我的预提交和 isort 配置:

.pre-commit-config.yaml

repos:
- repo: https://github.com/pycqa/isort
  rev: 5.8.0
  hooks:
    - id: isort
      args: ["--multi-line=5", "--line-length=120", "--use-parentheses", "--filter-files"]
      exclude: "migrations"
      stages: [commit]

tox.ini

[isort]
line_length=120
skip_glob=*migrations*
multi_line_output=5
sections=FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
use_parentheses=true
include_trailing_comma=true
lines_between_types=1
lines_after_imports=2
[testenv:isort]
deps =
    isort
commands =
    isort . --check-only --diff

Python 系统版本:3.10.1

Python docker 上的版本:3.8

感谢任何帮助!

这是 a bug in setuptools which has already been fixed (see also the pinned issue on isort)

您可以通过设置以下环境变量来解决此错误:SETUPTOOLS_USE_DISTUTILS=stdlib

setuptools 的版本来自您正在使用的 virtualenv 的版本,因此您可能需要升级以获得正确的版本

这里有更长的 summary of what went wrong with setuptools:

here's the relevant things to follow up on:

why we're suddenly seeing this:

  • the latest virtualenv release upgraded setuptools to 60.1.0 (despite what the changelog says it was upgraded to 60.1.0 here)
  • setuptools 60.* changes the default to using the setuptools-embedded distutils rather than the stdlib distutils
  • setuptools does this by way of a .pth file which redirects imports of distutils to setuptools._distutils
  • during pip's isolated builds (triggered by pyproject.toml, for example to install isort via poetry) pip attempts to clear the current sys.path of the enclosing environment and isolates to the pyproject.toml-installed build dependencies, but it's a bit too late as the .pth files from the enclosing environment are applied. so in this environment setuptools is not installed, but its import hooks are

免责声明:我创建了预提交