有没有办法让 isort 自动检测具有多个独立包的代码库中的第一方模块和第三方模块?

vin*_*tjp 8 python isort

project代码库中,标题为 的文件夹中有多个独立包plugins,每个包都在自己的文件夹中,setup.py其中有一个文件,项目本身是一个带有自己setup.py文件的 python 包。

我有两个文件夹project/projectplugins/myplugin_one/project_plugins/myplugin_one,我需要在适当的时候考虑first_party它们third_party。例如,在 里面plugins/myplugin_one/project_plugins/myplugin_one,有一个config.py包含以下代码的文件:

from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional

# First Party
from project.core.config_store import ConfigStore
Run Code Online (Sandbox Code Playgroud)

该导入from project.core.config_store import ConfigStore被视为first_party导入,但应将其视为third_party导入,因为该文件驻留在plugins/myplugin_one/project_plugins/myplugin_one并且myplugin_one是一个独立包 ( first_party),而projectthird_party此上下文中。

同样,对于驻留在 内的文件中的任何导入project/projectproject/project应考虑, 并应考虑first_party从 导入。plugins/myplugin_one/project_plugins/myplugin_onethird_party

该项目的顺序sections应该是:

sections=
    FUTURE
    STDLIB
    THIRDPARTY
    FIRSTPARTY
    LOCALFOLDER
Run Code Online (Sandbox Code Playgroud)

这是从 isort 4 到 isort 5.4.2 的升级,因此默认情况下不再是first_partythird_party并且__init__.py不会跳过默认部分。

这是我的isort.cfg文件:

[settings]
multi_line_output=3
include_trailing_comma=True
force_grid_wrap=0
use_parentheses=True
float_to_top = true
line_length=88
ensure_newline_before_comments=True
sections=
    FUTURE
    STDLIB
    THIRDPARTY
    FIRSTPARTY
    LOCALFOLDER
import_heading_stdlib=Standard Library
import_heading_firstparty=First Party
import_heading_thirdparty=Third Party
import_heading_localfolder=Local Folder
known_first_party=project,project_plugins
known_local_folder=build_helpers,tests
src_paths=
skip=
    __init__.py
Run Code Online (Sandbox Code Playgroud)

小智 0

isort 支持将某些软件包视为第一方或第三方软件包。这是通过配置文件中的选项完成的。在您的情况下,这取决于工作目录,哪些包应被视为第一或第三方包。isort支持指定位于不同目录中的多个配置文件。尽管这种方法不是全自动的,因为必须指定配置文件。