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

如何让isort识别配置文件中的line_length参数?

解决isort忽略pyproject.toml中line_length配置的问题

问题概述

配置isort与pre-commit、black协同工作时,pyproject.toml中指定的line_length = 90被忽略,isort始终使用默认79字符长度格式化导入语句,导致pre-commit钩子校验失败。命令行指定-w 90有效,但无法集成到pre-commit中。

可能的解决方案

1. 确保pyproject.toml位于项目根目录

isort默认从项目根目录加载配置文件,如果你的pyproject.toml放在子目录下,isort会找不到配置而使用默认值。将文件移到项目根目录,确保运行isort命令时的工作目录也是根目录。

2. 同步black与isort的line_length配置

当设置profile = "black"时,isort会自动继承black的line_length设置,此时单独配置isort的line_length会被覆盖。需要在pyproject.toml中同时配置black的行长度:

[tool.black]
line-length = 90

[tool.isort]
profile = "black"
multi_line_output = 3
ignore_trailing_comma = true
line_length = 90
skip = [".gitignore", ".dockerignore"]

这样isort会使用和black一致的90字符行长度,避免冲突。

3. 调整pre-commit钩子配置

pre-commit的isort钩子可能没有正确读取项目配置,可在.pre-commit-config.yaml中明确指定配置文件路径:

repos:
-   repo: https://github.com/PyCQA/isort
    rev: 5.12.0  # 使用最新稳定版本
    hooks:
    -   id: isort
        args: ["--settings-path", "./pyproject.toml"]

同时确保钩子使用的isort版本和本地环境一致,避免版本差异导致的配置读取问题。

4. 升级isort到最新稳定版

旧版本的isort对pyproject.toml的支持可能存在缺陷,执行以下命令升级:

pip install --upgrade isort

升级后重新运行isort和pre-commit校验,确认配置生效。


内容的提问来源于stack exchange,提问作者James Andrews

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 21:13:20