如何用Python Setuptools打包多个单个模块作为子包装

发布于 2025-02-01 02:19:42 字数 561 浏览 3 评论 0原文

我的目录结构看起来像是以下。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南街女流氓 2025-02-08 02:19:42

您已经部分回答了自己的问题:

我的__ INIT __。py是空白的。

导入软件包时,解释器会在软件包的__ Init __. Py文件中检查名称绑定。 empty __ init __。py文件仅提供同一目录中的子模块的名称绑定。例如,如果您的软件包结构

pkg_folder
└── pkg_name
    └── __init__.py

__ INT __。py包含一些定义,例如x = 2,则使用pkg_folder作为当前工作目录或安装到python环境的软件包,您可以导入x使用,

from pkg_name import x

但是,如果x在a subsodule ,情况有些不同。考虑以下软件包结构:

pkg_folder
└── pkg_name
    ├── __init__.py
    └── module1.py

导入pkg_name再次告诉解释器以检查pkg_name/__ Init __. py for name bindings。如果定义x = 2module1.py而不是__ init __. py中,那么它不是pkg_name 的范围;取而代之的是,它属于pkg_name.module1,因此以下功能:

from pkg_name.module1 import x

但是,您可以通过在__ Init __ Init中定义一个名称来明确地将其添加到pkg_name 。 >。在这种情况下,__ init __. py可能会读:

from .module1 import x

因为x现在在__ init __ init __. py中定义,允许我们在<<中定义。代码> pkg_name :同样的

from pkg_name import x

逻辑适用于子包装中的模块。因此,对于您要寻找的行为,您需要填充子包的__ init __. py文件,其中包含来自子包装中其他模块的软件包相对导入的文件。

from pkg_name.module1.module1 import func1

您的pkg_name/module1/__ init __。py文件

from pkg_name.module1 import func1

可能类似于以下内容:

from .module1 import func1

如果您有理由,您甚至可以将其扩展到传播func1 func1 父包。

You've partially answered your own question:

My __init__.py's are blank.

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 like

pkg_folder
└── pkg_name
    └── __init__.py

and __init__.py contains some definition like x = 2, then with pkg_folder as the current working directory or with the package installed to your Python environment, you can import x using

from pkg_name import x

However, if x is defined in a submodule, things are a little different. Consider the following package structure:

pkg_folder
└── pkg_name
    ├── __init__.py
    └── module1.py

Here, import pkg_name again tells the interpreter to check pkg_name/__init__.py for name bindings. If the definition x = 2 is in module1.py instead of __init__.py, then it is not part of pkg_name's scope; instead, it belongs to pkg_name.module1, so the following would work:

from pkg_name.module1 import x

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:

from .module1 import x

Which, since x is now defined in __init__.py, allows us to reference it under pkg_name as well:

from pkg_name import x

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

from pkg_name.module1.module1 import func1

to

from pkg_name.module1 import func1

your pkg_name/module1/__init__.py file might resemble something like the following:

from .module1 import func1

If you had reason to, you could even extend this to propagate func1 up to the parent package.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文