如何用Python Setuptools打包多个单个模块作为子包装
我的目录结构看起来像是以下。
pkg-folder
├── LICENSE
├── pkg-name
│ ├── __init__.py
│ ├── module1
│ │ ├── module1.py
│ │ ├── __init__.py
│ │ └── resourceFile.json
│ └── module2
│ ├── __init__.py
│ └── module2.py
├── README.md
├── setup.py
├── MANIFEST.ini
└── setup.cfg
构建和安装软件包后,我必须如下导入。
from pkg-name.module1.module1 import func1
当我知道Module1.py是子包中的一个文件时,我该如何直接导入如下所示?
from pkg-name.module1 import func1
我的__ INIT __。py's
是空白的。
My Directory structure looks like follows.
pkg-folder
├── LICENSE
├── pkg-name
│ ├── __init__.py
│ ├── module1
│ │ ├── module1.py
│ │ ├── __init__.py
│ │ └── resourceFile.json
│ └── module2
│ ├── __init__.py
│ └── module2.py
├── README.md
├── setup.py
├── MANIFEST.ini
└── setup.cfg
After building and installing the package I have to import as follows.
from pkg-name.module1.module1 import func1
How can I import directly like follows when i know module1.py will be a single file in the sub-package ?
from pkg-name.module1 import func1
My __init__.py's
are blank.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经部分回答了自己的问题:
导入软件包时,解释器会在软件包的
__ Init __. Py
文件中检查名称绑定。 empty__ init __。py
文件仅提供同一目录中的子模块的名称绑定。例如,如果您的软件包结构和
__ INT __。py
包含一些定义,例如x = 2
,则使用pkg_folder
作为当前工作目录或安装到python环境的软件包,您可以导入x
使用,但是,如果
x
在a subsodule ,情况有些不同。考虑以下软件包结构:导入pkg_name
再次告诉解释器以检查pkg_name/__ Init __. py
for name bindings。如果定义x = 2
在module1.py
而不是__ init __. py
中,那么它不是pkg_name 的范围;取而代之的是,它属于
pkg_name.module1
,因此以下功能:但是,您可以通过在
__ Init __ Init中定义一个名称来明确地将其添加到
pkg_name
。 >。在这种情况下,__ init __. py
可能会读:因为
x
现在在__ init __ init __. py
中定义,允许我们在<<中定义。代码> pkg_name :同样的逻辑适用于子包装中的模块。因此,对于您要寻找的行为,您需要填充子包的
__ init __. py
文件,其中包含来自子包装中其他模块的软件包相对导入的文件。从
您的
pkg_name/module1/__ init __。py
文件可能类似于以下内容:
如果您有理由,您甚至可以将其扩展到传播
func1
func1 父包。You've partially answered your own question:
When importing a package, the interpreter checks for name bindings in the package's
__init__.py
file. An empty__init__.py
file only provides name bindings for submodules within the same directory. For example, if you have a package structure likeand
__init__.py
contains some definition likex = 2
, then withpkg_folder
as the current working directory or with the package installed to your Python environment, you can importx
usingHowever, if
x
is defined in a submodule, things are a little different. Consider the following package structure:Here,
import pkg_name
again tells the interpreter to checkpkg_name/__init__.py
for name bindings. If the definitionx = 2
is inmodule1.py
instead of__init__.py
, then it is not part ofpkg_name
's scope; instead, it belongs topkg_name.module1
, so the following would work:However, you can explicitly add it to
pkg_name
by defining a name binding for it in__init__.py
with a package relative import. In this case,__init__.py
might read:Which, since
x
is now defined in__init__.py
, allows us to reference it underpkg_name
as well:The same logic applies to modules within sub-packages. So for the behavior you're looking for, you'll need to populate your sub-package's
__init__.py
files with package relative imports from the other modules within the sub-package.To go from
to
your
pkg_name/module1/__init__.py
file might resemble something like the following:If you had reason to, you could even extend this to propagate
func1
up to the parent package.