导入语句python3的变化 [英] Changes in import statement python3
问题描述
我不明白 pep-0404 中的以下内容><块引用>
在 Python 3 中,包内的隐式相对导入不再是可用 - 只有绝对导入和显式相对导入支持的.此外,明星导入(例如 from x import *)仅在模块级代码中允许.
什么是相对导入?python2 中还有哪些地方允许导入星号?请举例说明.
每当您导入相对于当前脚本/包的包时,就会发生相对导入.
以下面的树为例:
mypkg├── base.py└── 衍生.py
现在,您的 derived.py
需要来自 base.py
的东西.在 Python 2 中,你可以这样做(在 derived.py
中):
from base import BaseThing
Python 3 不再支持这一点,因为它并不明确您想要相对"还是绝对"base
.换句话说,如果系统中安装了一个名为 base
的 Python 包,你就会得到一个错误的包.
相反,它要求您使用显式导入,它在类似路径的基础上显式指定模块的位置.你的 derived.py
看起来像:
from .base import BaseThing
前导.
表示'import base
from module directory';换句话说,.base
映射到 ./base.py
.
同样,有 ..
前缀,它像 ../
一样在目录层次结构中上升(..mod
映射到 ../mod.py
),然后 ...
向上两级 (../../mod.py
) 等等
但是请注意,上面列出的相对路径是相对于当前模块 (derived.py
) 所在目录的,不是当前工作目录.
@BrenBarn 已经解释了明星导入案例.为了完整起见,我将不得不说同样的;)
例如,您需要使用几个 math
函数,但您只在单个函数中使用它们.在 Python 2 中,你可以半懒惰:
def sin_degrees(x):从数学导入 *返回罪(度(x))
请注意,它已经在 Python 2 中触发了警告:
a.py:1: SyntaxWarning: import * 仅允许在模块级别def sin_degrees(x):
在现代 Python 2 代码中你应该这样做,而在 Python 3 中你必须这样做:
def sin_degrees(x):from math import sin, degree返回罪(度(x))
或:
from math import *def sin_degrees(x):返回罪(度(x))
I don't understand the following from pep-0404
In Python 3, implicit relative imports within packages are no longer available - only absolute imports and explicit relative imports are supported. In addition, star imports (e.g. from x import *) are only permitted in module level code.
What is a relative import? In what other places star import was allowed in python2? Please explain with examples.
Relative import happens whenever you are importing a package relative to the current script/package.
Consider the following tree for example:
mypkg
├── base.py
└── derived.py
Now, your derived.py
requires something from base.py
. In Python 2, you could do it like this (in derived.py
):
from base import BaseThing
Python 3 no longer supports that since it's not explicit whether you want the 'relative' or 'absolute' base
. In other words, if there was a Python package named base
installed in the system, you'd get the wrong one.
Instead it requires you to use explicit imports which explicitly specify location of a module on a path-alike basis. Your derived.py
would look like:
from .base import BaseThing
The leading .
says 'import base
from module directory'; in other words, .base
maps to ./base.py
.
Similarly, there is ..
prefix which goes up the directory hierarchy like ../
(with ..mod
mapping to ../mod.py
), and then ...
which goes two levels up (../../mod.py
) and so on.
Please however note that the relative paths listed above were relative to directory where current module (derived.py
) resides in, not the current working directory.
@BrenBarn has already explained the star import case. For completeness, I will have to say the same ;).
For example, you need to use a few math
functions but you use them only in a single function. In Python 2 you were permitted to be semi-lazy:
def sin_degrees(x):
from math import *
return sin(degrees(x))
Note that it already triggers a warning in Python 2:
a.py:1: SyntaxWarning: import * only allowed at module level
def sin_degrees(x):
In modern Python 2 code you should and in Python 3 you have to do either:
def sin_degrees(x):
from math import sin, degrees
return sin(degrees(x))
or:
from math import *
def sin_degrees(x):
return sin(degrees(x))
这篇关于导入语句python3的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!