如何在 Python 中的子 package/module 的命名空间中获取父 module/package 函数?
How to get parent module/package functions in namespace of child package/module in Python?
我有一个极其复杂的模块,我想将子包分解成单独的包。我的第一次尝试是 "utilities" 子模块。我希望能够将父包 example_utils.py
中的所有内容导入 example_module.utils
,但我也希望 example_module.utils
也具有自己的功能。
最后我希望能够做到以下几点:
import example_module as em
x = 10
y1 = em.utils.f_parent1(x)
y2 = em.utils.f_child1(x)
# and do this
from example_module.utils import f_parent1, f_child1
# and use the parent module as a standalone
from example_utils import f_parent1, f_parent2
如何构造我的子模块 example_module
以具有此功能?**
模块实用程序另存为单独的独立模块example_utils.py
def f_parent1(x):
return x
def f_parent2(x,y):
return x+y
这个模块将安装在我的环境中:
pip install path/to/example_module
更大的模块 (example_module
) 使用 example_utils
作为依赖项
# Directory structure for larger Module
example_module
|++++| __init__.py
|++++| utils
|++++| ++++ | __init__.py
|++++| ++++ | utils.py
|++++| ++++ | __init__.py
的内容
from .utils import *
|++++| ++++ | utils.py
的内容
from example_utils import *
def f_child1(x):
return x**2
|++++| __init__.py
的内容
__version__= "0.1"
__developmental__ = True
# Utilities
from .utils import utils
# =======
# Direct Exports
# =======
_submodules = ["utils"]
__all__ = sorted(__all__)
如果 namespace
不正确,请提前致歉。我对 namespace
、scope
等感到困惑
根据@r-ook 的建议,我发现我可以使用 getattr
从父模块中按字符串名称获取函数。之后,我可以将函数添加到名称空间?范围?的子模块。
example.py
from example_utils import as emu
functions_from_parent = ["f_parent1", "f_parent2"]
__all__ = {"f_child1"}
for function_name in functions_from_parent:
globals()[function_name] = getattr(emu, function_name)
__all__.add(function_name)
__all__ = sorted(__all__)
def f_child1(x):
return x**2
我有一个极其复杂的模块,我想将子包分解成单独的包。我的第一次尝试是 "utilities" 子模块。我希望能够将父包 example_utils.py
中的所有内容导入 example_module.utils
,但我也希望 example_module.utils
也具有自己的功能。
最后我希望能够做到以下几点:
import example_module as em
x = 10
y1 = em.utils.f_parent1(x)
y2 = em.utils.f_child1(x)
# and do this
from example_module.utils import f_parent1, f_child1
# and use the parent module as a standalone
from example_utils import f_parent1, f_parent2
如何构造我的子模块 example_module
以具有此功能?**
模块实用程序另存为单独的独立模块example_utils.py
def f_parent1(x):
return x
def f_parent2(x,y):
return x+y
这个模块将安装在我的环境中:
pip install path/to/example_module
更大的模块 (example_module
) 使用 example_utils
作为依赖项
# Directory structure for larger Module
example_module
|++++| __init__.py
|++++| utils
|++++| ++++ | __init__.py
|++++| ++++ | utils.py
|++++| ++++ | __init__.py
from .utils import *
|++++| ++++ | utils.py
from example_utils import *
def f_child1(x):
return x**2
|++++| __init__.py
__version__= "0.1"
__developmental__ = True
# Utilities
from .utils import utils
# =======
# Direct Exports
# =======
_submodules = ["utils"]
__all__ = sorted(__all__)
如果 namespace
不正确,请提前致歉。我对 namespace
、scope
等感到困惑
根据@r-ook 的建议,我发现我可以使用 getattr
从父模块中按字符串名称获取函数。之后,我可以将函数添加到名称空间?范围?的子模块。
example.py
from example_utils import as emu
functions_from_parent = ["f_parent1", "f_parent2"]
__all__ = {"f_child1"}
for function_name in functions_from_parent:
globals()[function_name] = getattr(emu, function_name)
__all__.add(function_name)
__all__ = sorted(__all__)
def f_child1(x):
return x**2