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

Python中能否通过Black命令运行isort格式化工具?Black是否内部运行isort?

Can Black Run isort Internally?

Great question—let's break this down clearly:

1. Does Black run isort internally?

No, Black does not execute isort as part of its core functionality.

Black is focused solely on formatting code structure: things like line breaks, indentation, quote consistency, and spacing. Import sorting is a separate responsibility handled exclusively by isort. Even though you’ve set isort’s profile to "black" (which aligns isort’s output to match Black’s formatting rules), this only ensures compatibility between the two tools—it doesn’t make Black call isort under the hood.

That’s exactly why when you ran isort . after make format, it still found changes to make: Black hadn’t touched your import statements at all.

2. Can you run isort via the Black command?

You can’t directly trigger isort using the black command itself, but you can easily combine the two tools in your workflow to run them together seamlessly.

Since you’re already using a Makefile, update your format target to run isort first, then Black (to ensure any import sorting changes are properly formatted by Black):

format: clean
    @poetry run isort poetry/ tests/
    @poetry run black poetry/ tests/

This way, running make format will handle both import sorting and code formatting in one go.

Bonus: Keep them aligned

Your existing pyproject.toml config is on the right track with profile = "black" for isort—this ensures that isort’s import sorting won’t create formatting conflicts with Black. It’s a key step to keep both tools working smoothly together.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 03:34:08