Python:单模块包结构

发布于 2025-01-19 18:29:19 字数 1636 浏览 1 评论 0原文

我想应用包装python项目只有一个模块/.py文件。

我是Python的新手,所以我可能会误解这里的某些内容(但是数十年来编码),但是我看到的许多包装教程导致了必须访问的名称,这些名称必须像mypackage.mymodule.mymodule.xyz。他们显示的琐碎示例通常是samelongname.samelongname的形式。对于单个模块项目,这似乎是过度的。

我的主要目的是只为用户范围界定一个级别,即modulename

“ noreferrer”> packaging python projects 建议结构:

packaging_tutorial/
├── LICENSE
├── pyproject.toml
├── README.md
├── setup.cfg
├── src/
│   └── example_package/
│       ├── __init__.py
│       └── example.py
└── tests/

我特别喜欢放置的想法.py src下的文件清楚区分它。

对于一个模块,没有包装,我认为那是(摘录):

packaging_tutorial/
├── src/
│   └── example.py

他们提供的配置是(摘录):

[options]
package_dir =
    = src
packages = find:
python_requires = >=3.6

[options.packages.find]
where = src

但是,当我做python3 -m build时,.py .py < /代码>文件最终不会进入内置区域。

在找到链接的文档之前,我将.py保留在软件包root(即不在src下),并使用setup.py而不是setup.cfg这样:

from setuptools import setup
setup(
    py_modules=['mpvsilence']
)

这有效,但是当我试图使其更像文档时:

  • py_modules似乎在setup.cfg中不起作用。
  • py_modules = ['src/mpvsilence']似乎在设置中不起作用

。任何指导将不胜感激。

I'd like to apply the Packaging Python Projects process to a package with only one module/.py file.

I'm new to Python so I'm probably misunderstanding something here (but coding for decades), but many packaging tutorials I've seen lead to names that must be accessed like mypackage.mymodule.xyz. The trivial examples they show are often of the form samelongname.samelongname. For single module projects, that seems overkill.

My main objective is to just have one level of scoping for users i.e. modulename.

Packaging Python Projects suggests the structure:

packaging_tutorial/
├── LICENSE
├── pyproject.toml
├── README.md
├── setup.cfg
├── src/
│   └── example_package/
│       ├── __init__.py
│       └── example.py
└── tests/

I specifically like the idea of putting the .py file under src to clearly distinguish it.

For one module and no package, I was thinking that would be (excerpted):

packaging_tutorial/
├── src/
│   └── example.py

The configuration they offer is (excerpted):

[options]
package_dir =
    = src
packages = find:
python_requires = >=3.6

[options.packages.find]
where = src

However, when I do python3 -m build, the .py file does not end up in the built dist.

Before finding the linked docs, I was keeping the .py in the package root (i.e. not under src) and using a setup.py instead of a setup.cfg like this:

from setuptools import setup
setup(
    py_modules=['mpvsilence']
)

That worked, but when I tried to make it more like the docs:

  • py_modules doesn't seem to work in a setup.cfg.
  • py_modules=['src/mpvsilence'] didn't seem to work in the setup.py

I'm really just flopping around here. Any guidance would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文