Python 模块的绝对导入与显式相对导入
我想知道在 Python 应用程序中导入包的首选方法。我有一个像这样的包结构:
project.app1.models
project.app1.views
project.app2.models
project.app1.views
导入 project.app1.models
和 project.app2.models
。我想到了两种方法可以做到这一点。
使用绝对导入:
import A.A
import A.B.B
或使用显式相对导入,如 带有 PEP 328 的 Python 2.5:
# explicit relative
from .. import A
from . import B
执行此操作最 Pythonic 的方法是什么?
I'm wondering about the preferred way to import packages in a Python application. I have a package structure like this:
project.app1.models
project.app1.views
project.app2.models
project.app1.views
imports project.app1.models
and project.app2.models
. There are two ways to do this that come to mind.
With absolute imports:
import A.A
import A.B.B
or with explicit relative imports, as introduced in Python 2.5 with PEP 328:
# explicit relative
from .. import A
from . import B
What is the most pythonic way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不再强烈建议使用 Python 相对导入,但在这种情况下强烈建议使用absolute_import。
请参阅引用 Guido 本人的此讨论:
OP 正确链接了 PEP 328 说:
另请参阅几乎重复的问题 何时或为何在 Python 中使用相对导入
当然,这仍然是一个品味问题。虽然通过相对导入来移动代码更容易,但这也可能会意外地破坏事情;重命名导入并不那么困难。
要强制 PEP 328 使用新行为:
在这种情况下,将不再可能进行隐式相对导入(例如,
import localfile
将不再起作用,只能使用from .import localfile)。对于干净且面向未来的行为,建议使用absolute_import。
一个重要的警告是,由于 PEP 338 和 PEP 366,相对导入要求将 python 文件作为模块导入 - 您无法执行这样的 file.py具有相对导入,否则您将收到
ValueError: Attemptedrelative import in non-package
。在评估最佳方法时应考虑到这一限制。 Guido 反对在任何情况下从模块运行脚本:
关于此事的详尽讨论可以在 SO 上找到;关于。 Python 3 这是相当全面的:
Python relative imports are no longer strongly discouraged, but using absolute_import is strongly suggested in that case.
Please see this discussion citing Guido himself:
The OP correctly links the PEP 328 that says:
Also see almost duplicate question When or why to use relative imports in Python
Of course it still stands as a matter of taste. While it's easier to move code around with relative imports, that might also unexpectedly break things; and renaming the imports is not that difficult.
To force the new behaviour from PEP 328 use:
In this case, implicit relative import will no longer be possible (eg.
import localfile
will not work anymore, onlyfrom . import localfile
). For clean and future proof behaviour, using absolute_import is advisable.An important caveat is that because of PEP 338 and PEP 366, relative imports require the python file to be imported as a module - you cannot execute a file.py that has a relative import or you'll get a
ValueError: Attempted relative import in non-package
.This limitation should be taken into account when evaluating the best approach. Guido is against running scripts from a module in any case:
Exhaustive discussions on the matter can be found on SO; re. Python 3 this is quite comprehensive:
绝对进口。来自 PEP 8:
显式相对导入是一个很好的语言功能(我猜),但它们并不像绝对导入那么显式。更具可读性的形式是:
特别是当您导入多个不同的名称空间时。如果您查看一些编写良好的项目/教程,其中包含从包内导入的内容,它们通常遵循这种风格。
当其他人(也许还有你)试图找出你的命名空间时(特别是如果你迁移到 3.x,其中某些包名称已更改)。
Absolute imports. From PEP 8:
Explicit relative imports are a nice language feature (I guess), but they're not nearly as explicit as absolute imports. The more readable form is:
especially if you import several different namespaces. If you look at some well written projects/tutorials that include imports from within packages, they usually follow this style.
The few extra keystrokes you take to be more explicit will save others (and perhaps you) plenty of time in the future when they're trying to figure out your namespace (especially if you migrate to 3.x, in which some of the package names have changed).
相对导入不仅让您以后可以自由地重命名您的包,而无需更改数十个内部导入,而且我还成功地使用它们解决了涉及循环导入或命名空间包等的某些问题,因为它们不会将 Python“返回到top”以从顶级命名空间重新开始搜索下一个模块。
Relative imports not only leave you free to rename your package later without changing dozens of internal imports, but I have also had success with them in solving certain problems involving things like circular imports or namespace packages, because they do not send Python "back to the top" to start the search for the next module all over again from the top-level namespace.