"from ... import" 语句的确切语义是什么?

What is the exact semantic of "from ... import" starement?

我对这个说法进行了实验,但结果与官方描述不符。 Quotation 如下:

The from form uses a slightly more complex process:

  1. find the module specified in the from clause, loading and initializing it if necessary;
  2. for each of the identifiers specified in the import clauses:
    1. check if the imported module has an attribute by that name
    2. if not, attempt to import a submodule with that name and then check the imported module again for that attribute
    3. if the attribute is not found, ImportError is raised.
    4. otherwise, a reference to that value is stored in the local namespace, using the name in the as clause if it is present, otherwise using the attribute name

我创建了一个名为 l007 的命名空间包,其中名为 l009 的子模块在解释器中被 placed.I 键入 "from l007 import l009",执行正常,而在这种情况下应该会引发 ImportError。 我的理解有误吗?

参见this documentation:

When a submodule is loaded using any mechanism (e.g. importlib APIs, the import or import-from statements, or built-in __import__()) a binding is placed in the parent module’s namespace to the submodule object. For example, if package spam has a submodule foo, after importing spam.foo, spam will have an attribute foo which is bound to the submodule.

这就是为什么您的报价中的步骤按顺序排列的原因。即使 l007 最初没有属性 l009,它在子模块导入发生后会有一个.