我正在尝试构建一个包含多个子模块的库。一般结构如下:
package_test/
|___ setup.py
|___ README
|___ package_name/
|___|___ __init__.py
|___|___ submodule/
|___|___|___ __init__.py
|___|___|___ superawesome.py
|___|___ submodule2/
|___|___|___ __init__.py
|___|___|___ prettyokay.py
目标是允许用户执行以下操作:
from package_name.submodule import superawesome
from package_name.submodule2 import prettyokay
当前包含的每个子模块的__init__.py
(或子模块2的相应名称):
from .superawesome import superawesome
__all__ = ['superawesome']
内部package_name的__init__.py
包含:
__version__ = 'v0.0.alpha'
__all__ = ['submodule','submodule2']
当我尝试使用它时,iPython会显示以下错误:
From package_name.submodule import superawesome
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-03be4241edd5> in <module>()
----> 1 from package_name.submodule import superawesome
ModuleNotFoundError: No module named 'package_name.submodule'
我的setup.py
目前包含:
from distutils.core import setup
setup(
name = 'package_name',
packages = ['package_name'], # this must be the same as the name above
version = '0.1.1',
long_description=open('README').read(),
)
我需要帮助搞清楚如何进行分层导入结构,以便将子模块正确导入为整个模块的一部分。这显然是一个人为的命名集,但在实际问题中,每个子模块都包含一组属于一起的模块......但所有子模块都是库所必需的;所以我真的想保持这种结构。