简体   繁体   English

为什么即使未定义__all__,pkg import *最终导入模块?

[英]Why does from pkg import * end up importing a module even if __all__ is not defined?

I have created a package pkg in this manner. 我以这种方式创建了一个包pkg

$ tree
.
└── pkg
    ├── foo.py
    └── __init__.py

1 directory, 2 files
susam@debian1:~/so$ cat pkg/__init__.py
susam@debian1:~/so$ cat pkg/foo.py
print('executing module foo ...')

def bar():
    print('bar')

All the Python shell snippets below are from a single interactive session with the Python interpreter. 下面的所有Python shell代码段都来自与Python解释器的单个交互式会话。 I have split them up into multiple blocks to add my own commentary in between. 我把它们拆分成多个块,在两者之间添加我自己的评论。

Here is my Python version. 这是我的Python版本。

Python 3.4.2 (default, Oct  8 2014, 10:45:20)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

The following import does not import foo because __all__ is not defined in __init__.py . 以下导入不导入foo因为在__init__.py未定义__all__

>>> from pkg import *
>>> foo.bar()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined
>>>

The above behaviour has been described in the Python tutorial at https://docs.python.org/3/tutorial/modules.html#importing-from-a-package . 以上行为已在Python教程中描述, 网址https://docs.python.org/3/tutorial/modules.html#importing-from-a-package

If __all__ is not defined, the statement from sound.effects import * does not import all submodules from the package sound.effects into the current namespace; 如果未定义__all__ ,则from sound.effects import *中的语句不会将包sound.effects所有子模块导入当前名称空间; it only ensures that the package sound.effects has been imported (possibly running any initialization code in __init__.py ) and then imports whatever names are defined in the package. 它只确保已导入包sound.effects (可能在__init__.py运行任何初始化代码),然后导入包中定义的任何名称。

The following imports only bar() . 以下只导入bar() It does not import foo . 它不会导入foo

>>> from pkg.foo import bar
executing module foo ...
>>> bar()
bar
>>> foo.bar()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined

But strangely after the previous import, the following import ends up importing foo even if __all__ is not defined in __init__.py . 但奇怪的是,在上一次导入之后,即使在__init__.py未定义__all__ ,以下导入也会导致导入foo

>>> from pkg import *
>>> foo.bar()
bar

Why does this happen? 为什么会这样?

Based off of the Python documentation that you cited, when you use from pkg import * you are importing only the module foo as well as __init__.py and not the functions of foo . 根据您引用的Python文档,当您from pkg import *使用时from pkg import *您只导入模块foo__init__.py而不是foo的函数。

I suspect the last code snippet you provided works because you have already imported bar() directly from foo and reimporting foo after you've strictly imported the bar function establishes the connection between foo and bar() . 我怀疑你提供的最后一个代码片段是有效的,因为你已经直接从foo导入了bar()并在你严格导入bar函数后重新导入foo ,建立了foobar()之间的连接。

Do you run into this same issue when specifying __all__ ? 指定__all__时是否遇到同样的问题?

foo is already imported in pkg namespace as pkg.foo when you do from pkg.foo import bar . 当你from pkg.foo import bar执行pkg.foo时, foo已经作为pkg.foo导入到pkg命名空间中。 So when you then import all names from pkg namespace to current scope you also import foo . 因此,当您将所有名称从pkg命名空间导入当前范围时,您还会导入foo See: 看到:

>>> import pkg
>>> dir(pkg)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']
>>> from pkg.foo import bar
executing module foo ...
>>> dir(pkg)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'foo']

(I am on python 2 but the logic for this is the same) (我在python 2上,但这个逻辑是一样的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM