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

问题描述

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

我有两个文件夹,project/projectplugins/myplugin_one/project_plugins/myplugin_one,我需要在适当first_partythird_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

导入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

这是从 isort 4 到 isort 5.4.2 的升级,因此默认部分不再first_party,但默认情况third_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

标签: pythonisort

解决方案


推荐阅读