python isort工具第一方导入和第三方导入问题

huangapple go评论70阅读模式
英文:

python isort utility 1st party import and 3rd party imports issue

问题

以下是您的项目目录结构:

Project_Name
|-project_name
|--__init__.py
|--file1.py
|--file2.py
|--main.py
|-tests
|-docs
|-pyproject.toml

我的isort配置位于根目录Project_Name/pyproject.toml,而.py文件位于子目录Project_Name/project_name/xxx.py中。

如果我像下面这样导入,比如从main.py

from file1 import xyz
from file2 import abc

isort会将file1和file2分类为第三方导入,而不是同一应用程序的文件,即第一方导入。

如果我将pyproject.toml文件放入Project_Name/project_name/中,与.py文件一起,然后它可以工作,file1和file2现在被视为第一方导入。

我尝试将以下内容添加到配置文件中 known_first_party = ['project_name'],但即使有了这个,file1和file2仍然被视为第三方导入。
有没有办法将pyproject.toml保留在根目录,并仍然将file1和file2标识为第一方导入?

以下是我的pyproject.toml配置:

[tool.isort]
line_length = 120
use_parentheses = true
multi_line_output = 3
ensure_newline_before_comments = true
include_trailing_comma = true
split_on_trailing_comma = true
float_to_top = true
indented_import_headings = false
known_future_library = ['__future__', 'config']
extra_standard_library = []
known_third_party = []
known_mylib = ['my_utils_library']
known_first_party = []
known_local_folder = []
英文:

I have a project directory structure as follows:

Project_Name
|-project_name
|--__init__.py
|--file1.py
|--file2.py
|--main.py
|-tests
|-docs
|-pyproject.toml

My isort config is in the root Project_Name/pyproject.toml and the .py files are in the subdir Project_Name/project_name/xxx.py

If I import as follows, from let's say main.py

from file1 import xyz
from file2 import abc

isort will classify file1 and file2 as third party imports as opposed to first party which are files of the same application.

If I drop the pyproject.toml into the Project_Name/project_name/, among the .py files, then it works and file1 and file2 are now addressed as first party imports.

I tried adding this to the config file known_first_party = ['project_name'] but even with that file1 and file2 are still considered as third party imports.
Is there a way to leave the pyproject.toml in the root dir and still get file1 and file2 identified as first party?

here is my pyproject.toml

[tool.isort]
line_length = 120
use_parentheses = true
multi_line_output = 3
ensure_newline_before_comments = true
include_trailing_comma = true
split_on_trailing_comma = true
float_to_top = true
indented_import_headings = false
known_future_library = ['__future__', 'config']
extra_standard_library = []
known_third_party = []
known_mylib = ['my_utils_library']
known_first_party = []
known_local_folder = []

答案1

得分: 0

在你的 pyproject.toml 文件的 [tool.isort] 部分添加一个指令:

src_paths = ["project_name"]

如果没有这个指令,isort 会认为你应该使用 from project_name.file1 import xyz,而当它看到 from file1 时,会认为那必须是一个第三方库。

请参阅 https://pycqa.github.io/isort/docs/configuration/config_files.html#pyprojecttoml-preferred-format

英文:

add a directive to the [tool.isort] section of your pyproject.toml:

src_paths = ["project_name"]

Without that isort thinks you should be doing from project_name.file1 import xyz and when it sees from file1 it thinks that must be a 3rd party lib.

See https://pycqa.github.io/isort/docs/configuration/config_files.html#pyprojecttoml-preferred-format

huangapple
  • 本文由 发表于 2023年7月20日 19:33:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定