什么是。在Python中的import语句中是什么意思?

问题:什么是。在Python中的import语句中是什么意思?

我正在查看Python multiprocessing模块的代码,其中包含以下行:

from ._multiprocessing import win32, Connection, PipeConnection

代替

from _multiprocessing import win32, Connection, PipeConnection

细微的差别是前一个时期_multiprocessing。那是什么意思?为什么要月经?

I’m looking over the code for Python’s multiprocessing module, and it contains this line:

from ._multiprocessing import win32, Connection, PipeConnection

instead of

from _multiprocessing import win32, Connection, PipeConnection

the subtle difference being the period before _multiprocessing. What does that mean? Why the period?


回答 0

这是显式相对导入的新语法。这意味着从当前包导入。

That’s the new syntax for explicit relative imports. It means import from the current package.


回答 1

模块名称中的点用于相对模块导入(请参见此处此处,第6.4.2节)。

您可以使用多个点,而不是指当前软件包,而是指其父对象。这仅应在包内使用,在主模块中,应始终使用绝对模块名称。

The dot in the module name is used for relative module import (see here and here, section 6.4.2).

You can use more than one dot, referring not to the curent package but its parent(s). This should only be used within packages, in the main module one should always use absolute module names.