导入语句python3中的更改

问题:导入语句python3中的更改

我不了解pep-0404的以下内容

在Python 3中,包内的隐式相对导入不再可用-仅支持绝对导入和显式相对导入。此外,仅在模块级别代码中允许星号导入(例如,从x import *导入)。

什么是相对进口?在python2中还允许在其他什么地方导入星号?请举例说明。

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.


回答 0

每当导入相对于当前脚本/软件包的软件包时,就会进行相对导入。

例如,考虑以下树:

mypkg
├── base.py
└── derived.py

现在,您derived.py需要从中获得一些东西base.py。在Python 2中,您可以这样做(在中derived.py):

from base import BaseThing

Python 3不再支持该功能,因为它是否明确要求“相对”还是“绝对” base。换句话说,如果base系统中安装了一个名为Python的软件包,那么您将得到错误的软件包。

相反,它要求您使用显式导入,这些显式导入在类似路径的基础上显式指定模块的位置。您derived.py将看起来像:

from .base import BaseThing

领导.说“ base从模块目录导入”;换句话说,.base映射到./base.py

同样,有一个..前缀沿目录层次结构向上../(如..mod映射到../mod.py),然后沿...两个层次向上(../../mod.py),依此类推。

但是请注意,上面列出的相对路径是相对于当前模块(derived.py)所在的目录的,而不是相对于当前工作目录的。


@BrenBarn已经解释了star导入案例。为了完整性,我将不得不说相同;)。

例如,您需要使用一些math功能,但只能在单个功能中使用它们。在Python 2中,您被允许是半懒惰的:

def sin_degrees(x):
    from math import *
    return sin(degrees(x))

请注意,它已经在Python 2中触发了警告:

a.py:1: SyntaxWarning: import * only allowed at module level
  def sin_degrees(x):

在现代Python 2代码中,您应该这样做,而在Python 3中,您必须执行以下任一操作:

def sin_degrees(x):
    from math import sin, degrees
    return sin(degrees(x))

要么:

from math import *

def sin_degrees(x):
    return sin(degrees(x))

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))

回答 1

有关相对进口的信息,请参阅文档。相对导入是指从模块中相对于该模块位置的导入,而不是绝对从中导入sys.path

至于import *,Python 2允许在函数内导入星形,例如:

>>> def f():
...     from math import *
...     print sqrt

在Python 2(至少是最新版本)中为此发出警告。在Python 3中,它不再被允许,并且您只能在模块的顶层(而不是在函数或类内部)进行星形导入。

For relative imports see the documentation. A relative import is when you import from a module relative to that module’s location, instead of absolutely from sys.path.

As for import *, Python 2 allowed star imports within functions, for instance:

>>> def f():
...     from math import *
...     print sqrt

A warning is issued for this in Python 2 (at least recent versions). In Python 3 it is no longer allowed and you can only do star imports at the top level of a module (not inside functions or classes).


回答 2

要同时支持Python 2和Python 3,请使用如下所示的显式相对导入。它们相对于当前模块。从2.5开始支持它们。

from .sister import foo
from . import brother
from ..aunt import bar
from .. import uncle

To support both Python 2 and Python 3, use explicit relative imports as below. They are relative to the current module. They have been supported starting from 2.5.

from .sister import foo
from . import brother
from ..aunt import bar
from .. import uncle

回答 3

在MichałGórny的答案中添加了另一个案例:

请注意,相对导入基于当前模块的名称。由于主模块的名称始终为“ __main__”,因此用作Python应用程序主模块的模块必须始终使用绝对导入。

Added another case to Michał Górny’s answer:

Note that relative imports are based on the name of the current module. Since the name of the main module is always “__main__“, modules intended for use as the main module of a Python application must always use absolute imports.