Python 从包中导入函数
Python import function from package
(Python 3.6)
我有这个文件夹结构:
package/
start.py
subpackage/
__init__.py
submodule.py
submodule.py:
def subfunc():
print("This is submodule")
__ 初始化__.py:
from subpackage.submodule import subfunc
start.py:
import subpackage
subpackage.subfunc()
subpackage.submodule.subfunc()
我明白如何以及为什么
subpackage.subfunc()
有效。
但我不明白为什么:
subpackage.submodule.subfunc()
也可以,如果我没做过的话:
from subpackage import submodule
也不是:
import subpackage.submodule
既不在 __ init __.py 也不在 start.py
非常感谢,如果有人能解开我的疑惑。
如果我没猜错,你有一个名为 "package" 的文件夹,其中有 2 个 东西 :一个 .py 文件和另一个名为 "subpackage".
在 "subpackage" 里面你有 __init__.py
和 submodule.py
后者包含一个只打印 "This is submodule".
的函数
现在,当你调用 import subpackage
时,你会调用 "pull" "subpackage"[=27 中的所有内容 =],包括子模块,因此,subfunc()
函数。
当你写 subpackage.submodule.subfunc()
时,那里真的没什么了不起的,你只需调用 mainfolder/container (子包。),然后是 .py 文件 ( submodule.) 最后是函数本身 (subfunc() ).
发布from subpackage.submodule import subfunc
时,python为你做了two things:一、搜索并评估名为subpackage.submodule
的模块,将其放入sys.modules
缓存;二、填充subpackage.submodule.subfunc
对象并将名称"subfunc"绑定到当前模块的命名空间:
The import statement combines two operations; it searches for the named module, then it binds the results of that search to a name in the local scope.
导入 subpackage.submodule
时,submodule
的父级也被导入:
While certain side-effects may occur, such as the importing of parent packages, and the updating of various caches (including sys.modules) ...
在导入 subpackage.submodule
的最后阶段,python 将设置模块 as an attribute on its parent subpackage
, this behavior is documented:
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.
(Python 3.6)
我有这个文件夹结构:
package/
start.py
subpackage/
__init__.py
submodule.py
submodule.py:
def subfunc():
print("This is submodule")
__ 初始化__.py:
from subpackage.submodule import subfunc
start.py:
import subpackage
subpackage.subfunc()
subpackage.submodule.subfunc()
我明白如何以及为什么
subpackage.subfunc()
有效。
但我不明白为什么:
subpackage.submodule.subfunc()
也可以,如果我没做过的话:
from subpackage import submodule
也不是:
import subpackage.submodule
既不在 __ init __.py 也不在 start.py
非常感谢,如果有人能解开我的疑惑。
如果我没猜错,你有一个名为 "package" 的文件夹,其中有 2 个 东西 :一个 .py 文件和另一个名为 "subpackage".
在 "subpackage" 里面你有 __init__.py
和 submodule.py
后者包含一个只打印 "This is submodule".
现在,当你调用 import subpackage
时,你会调用 "pull" "subpackage"[=27 中的所有内容 =],包括子模块,因此,subfunc()
函数。
当你写 subpackage.submodule.subfunc()
时,那里真的没什么了不起的,你只需调用 mainfolder/container (子包。),然后是 .py 文件 ( submodule.) 最后是函数本身 (subfunc() ).
发布from subpackage.submodule import subfunc
时,python为你做了two things:一、搜索并评估名为subpackage.submodule
的模块,将其放入sys.modules
缓存;二、填充subpackage.submodule.subfunc
对象并将名称"subfunc"绑定到当前模块的命名空间:
The import statement combines two operations; it searches for the named module, then it binds the results of that search to a name in the local scope.
导入 subpackage.submodule
时,submodule
的父级也被导入:
While certain side-effects may occur, such as the importing of parent packages, and the updating of various caches (including sys.modules) ...
在导入 subpackage.submodule
的最后阶段,python 将设置模块 as an attribute on its parent subpackage
, this behavior is documented:
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.