亲戚进口的排序顺序?
Sort order of relatives imports?
在 python 中对 相对导入 进行排序的正确方法或 PEP 方法是什么?
core/
__init__.py
forms.py
models.py
tests/
__init__.py
form_tests/
__init__.py
test_xpto.py
login.py
如果我正在使用 test_xpto.py
并想导入其他文件,正确的方法是什么:
from core.models import Person
from ..login import DefaultLogin
from ...forms import CustomerForm
或
from ...forms import CustomerForm
from ..login import DefaultLogin
from core.models import Person
或者不是其中任何一个?
我得到了这个问题的一些答案。根据 PEP 328 [1],Guido 是否发音为 "relative imports will use leading dots. A single leading dot indicates a relative import, starting with the current package. Two or more leading dots give a relative import to the parent(s) of the current package, one level per dot after the first" [2]。这是一个示例包布局:
from .moduleY import spam
from .moduleY import spam as ham
from . import moduleY
from ..subpackage1 import moduleY
from ..subpackage2.moduleZ import eggs
from ..moduleA import foo
from ...package import bar
from ...sys import path
另一个好主意是 isort(一个 Python 实用程序/库,用于按字母顺序对导入进行排序,并自动分成多个部分)[3],它对导入进行排序遵循了另一种模式,它被像 Django 这样的大项目 [4]:
from __future__ import absolute_import
import os
import sys
from my_lib import Object, Object2, Object3
from third_party import (lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9,
lib10, lib11, lib12, lib13, lib14, lib15)
from . import moduleY
from ...sys import path
from ..subpackage1 import moduleY
from ..subpackage2.moduleZ import eggs
from .moduleY import spam as ham
[1] https://www.python.org/dev/peps/pep-0328/#guido-s-decision
[2] https://mail.python.org/pipermail/python-dev/2004-March/043739.html
[3] https://github.com/timothycrosley/isort
[4]https://github.com/timothycrosley/isort/wiki/Projects-Using-isort
在 python 中对 相对导入 进行排序的正确方法或 PEP 方法是什么?
core/
__init__.py
forms.py
models.py
tests/
__init__.py
form_tests/
__init__.py
test_xpto.py
login.py
如果我正在使用 test_xpto.py
并想导入其他文件,正确的方法是什么:
from core.models import Person
from ..login import DefaultLogin
from ...forms import CustomerForm
或
from ...forms import CustomerForm
from ..login import DefaultLogin
from core.models import Person
或者不是其中任何一个?
我得到了这个问题的一些答案。根据 PEP 328 [1],Guido 是否发音为 "relative imports will use leading dots. A single leading dot indicates a relative import, starting with the current package. Two or more leading dots give a relative import to the parent(s) of the current package, one level per dot after the first" [2]。这是一个示例包布局:
from .moduleY import spam
from .moduleY import spam as ham
from . import moduleY
from ..subpackage1 import moduleY
from ..subpackage2.moduleZ import eggs
from ..moduleA import foo
from ...package import bar
from ...sys import path
另一个好主意是 isort(一个 Python 实用程序/库,用于按字母顺序对导入进行排序,并自动分成多个部分)[3],它对导入进行排序遵循了另一种模式,它被像 Django 这样的大项目 [4]:
from __future__ import absolute_import
import os
import sys
from my_lib import Object, Object2, Object3
from third_party import (lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9,
lib10, lib11, lib12, lib13, lib14, lib15)
from . import moduleY
from ...sys import path
from ..subpackage1 import moduleY
from ..subpackage2.moduleZ import eggs
from .moduleY import spam as ham
[1] https://www.python.org/dev/peps/pep-0328/#guido-s-decision
[2] https://mail.python.org/pipermail/python-dev/2004-March/043739.html
[3] https://github.com/timothycrosley/isort
[4]https://github.com/timothycrosley/isort/wiki/Projects-Using-isort