You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行pre-commit时出现AttributeError: 'EntryPoints' object has no attribute 'get'

问题

执行pre-commit install时触发AttributeError: 'EntryPoints' object has no attribute 'get'错误,错误由flake8引发。已尝试安装importlib_metadata==4.13.0但问题未解决。

错误栈信息

File "/Users/X/.cache/pre-commit/repo_fr5yg6d/py_env-python3.7/lib/python3.7/site-packages/flake8/plugins/manager.py", line 254, in _load_entrypoint_plugins
    eps = importlib_metadata.entry_points().get(self.namespace, ())
AttributeError: 'EntryPoints' object has no attribute 'get'

当前pre-commit配置文件

exclude: 'settings.py'
fail_fast: true
repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks.git
    rev: v4.0.1
    hooks:
    -   id: trailing-whitespace
    -   id: check-yaml
    -   id: check-json
    -   id: check-docstring-first
    -   id: requirements-txt-fixer
    -   id: debug-statements
    -   id: check-toml
    -   id: pretty-format-json
        args: [--autofix]
    -   id: no-commit-to-branch
        args: [--branch, develop, --branch, master]

-   repo: https://github.com/pycqa/isort
    rev: 5.11.5
    hooks:
    -   id: isort
        name: isort
        exclude: ^site-pacakges/

-   repo: https://github.com/asottile/pyupgrade
    rev: v2.26.0
    hooks:
    -   id: pyupgrade
        exclude: ^site-pacakges/
        args: ["--py37-plus"]

-   repo: https://github.com/psf/black
    rev: 23.1.0
    hooks:
    -   id: black
        exclude: ^site-pacakges/
        language_version: python3.7

-   repo: https://github.com/PyCQA/flake8
    rev: 3.9.2
    hooks:
    -   id: flake8
        additional_dependencies: [flake8-typing-imports==1.10.0]
        exclude: ^site-pacakges/

-   repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.910
    hooks:
    -   id: mypy
        name: Mypy typing checks
        args: ["--config-file=pyproject.toml"]
        exclude: ^site-pacakges/

-   repo: local
    hooks:
    -   id: tests
        name: run tests
        entry: pytest
        pass_filenames: false
        language: system

解决方法

核心原因

pre-commit为每个钩子创建独立的隔离环境,全局安装的importlib_metadata不会被flake8的钩子环境复用,所以之前的操作无法生效。

具体步骤

  1. 修改flake8的依赖配置
    在pre-commit配置文件中,给flake8的additional_dependencies添加指定版本的importlib_metadata,同时修正拼写错误:

    -   repo: https://github.com/PyCQA/flake8
        rev: 3.9.2
        hooks:
        -   id: flake8
            additional_dependencies: [flake8-typing-imports==1.10.0, importlib_metadata==4.13.0]
            exclude: ^site-packages/  # 修正拼写:site-pacakges → site-packages
    
  2. 清理pre-commit缓存
    删除旧的钩子环境,确保新依赖被正确安装:

    pre-commit clean
    
  3. 重新初始化并验证
    重新执行安装命令,然后测试问题是否解决:

    pre-commit install
    pre-commit run --all-files
    

内容的提问来源于stack exchange,提问作者raj-kapil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 13:03:32