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

Mur*_*dam 3 python pre-commit pre-commit-hook isort pre-commit.com

我正在尝试运行预提交挂钩,但是当它们遇到 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'
Run Code Online (Sandbox Code Playgroud)

我正在使用 docker,并且我已经检查了 setuptools 是否已安装在我的全局计算机和 docker 上。我不明白为什么会出现这个错误。我认为 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]
Run Code Online (Sandbox Code Playgroud)

毒物文件

[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
Run Code Online (Sandbox Code Playgroud)

系统上的Python版本:3.10.1

docker 上的 Python 版本:3.8

我很感激任何帮助!

Ant*_*ile 7

这是setuptools 中的一个错误,已修复(另请参阅上的固定问题isort

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

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

以下是setuptools 问题的详细摘要:

以下是需要跟进的相关事项:

为什么我们突然看到这个:

  • 最新的 virtualenv 版本将 setuptools 升级到 60.1.0(尽管变更日志说它升级到 60.1.0
  • setuptools 60.* 将默认值更改为使用 setuptools-embeddeddistutils而不是 stdlib distutils
  • setuptools 通过一个.pth文件来完成此操作,该文件将导入重定向distutilssetuptools._distutils
  • 在 pip 的隔离构建期间(由 触发pyproject.toml,例如isort通过进行安装poetry),pip 尝试清除当前sys.path的封闭环境并隔离到 pyproject.toml 安装的构建依赖项,但有点太晚了,因为.pth来自封闭环境的文件应用。所以在这个环境中setuptools没有安装,但是它的导入钩子是

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