python中的绝对导入是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22678919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is absolute import in python?
提问by Shiva
I am new to Python. I am developing a small project. I need to follow coding standards from starting on wards. How to use importstatements in a proper way. Now I am working on Python 2.7. If I move to 3.x are there any conflicts with absolute imports? And what is the difference between absolute and relative imports?
我是 Python 的新手。我正在开发一个小项目。我需要从病房开始就遵循编码标准。如何import正确使用语句。现在我正在研究 Python 2.7。如果我移动到 3.x 是否与绝对导入有任何冲突?绝对进口和相对进口有什么区别?
采纳答案by Two-Bit Alchemist
The distinction between absoluteand relativethat's being drawn here is very similar to the way we talk about absolute and relative file paths or even URLs.
这里绘制的absolute和之间的区别与relative我们谈论绝对和相对文件路径甚至 URL 的方式非常相似。
An absolute {import, path, URL} tells you exactlyhow to get the thing you are after, usually by specifying every part:
绝对{进口,路径,URL}告诉你究竟如何让你以后的事情,通常通过指定每一个部分:
import os, sys
from datetime import datetime
from my_package.module import some_function
Relative {imports, paths, URLs} are exactly what they say they are: they're relativeto their current location. That is, if the directory structure changes or the file moves, these may break (because they no longer mean the same thing).
相对 {imports, paths, URLs} 正是他们所说的:它们相对于它们的当前位置。也就是说,如果目录结构更改或文件移动,这些可能会中断(因为它们不再意味着相同的事情)。
from .module_in_same_dir import some_function
from ..module_in_parent_dir import other_function
Hence, absolute imports are preferred for code that will be shared.
因此,对于将要共享的代码,绝对导入是首选。
I was asked in comments to provide an example of how from __future__ import absolute_importties into this, and how it is meant to be used. In trying to formulate this example, I realized I couldn't quite explain its behavior either, so I asked a new question. This answergives a code sample showing a correctly working implementation of from __future__ import absolute_import, where it actually resolves an ambiguity.
我在评论中被要求提供一个示例,说明如何from __future__ import absolute_import与此联系以及如何使用它。在试图表述这个例子时,我意识到我也不能完全解释它的行为,所以我问了一个新问题。这个答案给出了一个代码示例,显示了 的正确工作实现from __future__ import absolute_import,它实际上解决了歧义。
The accepted answergoes into more detail about why this works the way it does, including a discussion of the confusing wording of the Python 2.5 changelog. Essentially, the scope of this directive (and by extension the distinction between absolute and relative imports in Python) is very, very narrow. If you find yourself needing these distinctions to make your code work, you're probably better off renaming your local module if at all possible.
接受的答案更详细地说明了为什么会这样,包括对 Python 2.5 变更日志令人困惑的措辞的讨论。本质上,该指令的范围(以及 Python 中绝对导入和相对导入之间的扩展)非常非常狭窄。如果您发现自己需要这些区别才能使您的代码正常工作,则最好尽可能重命名本地模块。
回答by Deepak Gupta
Imports should usually be on separate lines:
导入通常应该在单独的行上:
Yes: import os
import sys
是的: import os
import sys
No: import sys, os
不: import sys, os
It's okay to say this though:
不过可以这么说:
from subprocess import Popen, PIPEImports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
from subprocess import Popen, PIPE导入总是放在文件的顶部,就在任何模块注释和文档字符串之后,以及模块全局变量和常量之前。
Imports should be grouped in the following order:
进口应按以下顺序分组:
- Standard library imports.
- Related third party imports.
- Local application/library specific imports.
- You should put a blank line between each group of imports.
- 标准库导入。
- 相关的第三方进口。
- 本地应用程序/库特定的导入。
- 您应该在每组导入之间放置一个空行。
As per Pep8 :- Absolute imports are recommended, as they are usually more readable and tend to be better behaved (or at least give better error messages) if the import system is incorrectly configured (such as when a directory inside a package ends up on sys.path):
根据 Pep8 :- 建议使用绝对导入,因为如果导入系统配置不正确(例如当包内的目录最终出现在sys.path):
import mypkg.sibling
from mypkg import sibling
from mypkg.sibling import example
However, explicit relative imports are an acceptable alternative to absolute imports, especially when dealing with complex package layouts where using absolute imports would be unnecessarily verbose:
但是,显式相对导入是绝对导入的可接受替代方案,尤其是在处理复杂的包布局时,使用绝对导入会不必要地冗长:
from . import sibling
from .sibling import example
Standard library code should avoid complex package layouts and always use absolute imports.
标准库代码应避免复杂的包布局并始终使用绝对导入。
Implicit relative imports should never be used and have been removed in Python 3.
不应使用隐式相对导入,并且已在 Python 3 中删除。
When importing a class from a class-containing module, it's usually okay to spell this:
从包含类的模块导入类时,通常可以这样拼写:
from myclass import MyClass
from foo.bar.yourclass import YourClass
If this spelling causes local name clashes, then spell them explicitly:
如果此拼写导致本地名称冲突,则明确拼写它们:
import myclass
import foo.bar.yourclass
and use "myclass.MyClass" and "foo.bar.yourclass.YourClass".
并使用“myclass.MyClass”和“foo.bar.yourclass.YourClass”。
Wildcard imports (from <module> import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools. There is one defensible use case for a wildcard import, which is to republish an internal interface as part of a public API (for example, overwriting a pure Python implementation of an interface with the definitions from an optional accelerator module and exactly which definitions will be overwritten isn't known in advance).
from <module> import *应避免通配符导入 ( ),因为它们会使命名空间中存在哪些名称变得不清楚,从而使读者和许多自动化工具都感到困惑。通配符导入有一个可防御的用例,即重新发布内部接口作为公共 API 的一部分(例如,使用可选加速器模块中的定义覆盖接口的纯 Python 实现以及哪些定义将被被覆盖是事先不知道的)。

