也有接口模块的 python 库的一般约定?

发布于 2024-09-27 15:37:26 字数 943 浏览 8 评论 0原文

现在我有一个具有以下布局的项目:

foo/
  __init__.py
  __main__.py
  foo.py

在这种情况下,foo.py实际上是主api文件,因此开发人员应该执行“from foo import foo”,但我还想让最终用户只需运行 ~$ foo 并获得一个界面。

当我安装 distutils 时,会创建 /usr/bin/__main__.py 因为 (a) 我不知道如何使用 distutils,[不太重要] 并且 (b) 我不知道确定什么通常被认为是正确的事情。

据我所知,我有三个选择:

  1. 使 distutils 更智能,以便 setup.py install 创建 符号链接 /usr/bin/foo -> $PYTHONLIB/foo/__main__.py。这是我的 直接的直觉,我也许能弄清楚该怎么做, 虽然我想做的事情都感觉像是黑客 我还没有发现有人谈论这个。

  2. 在分发之前将 __main__.py 重命名为 foo,并将调用修改为 distutils 的设置为 setup(scripts=['foo'], ...)。这与 (1) 非常相似,除了它发生的时间,我认为。

  3. 只是不要在库包中包含接口。我觉得 像这样主要取决于库/接口的大小 看看它是否有意义。

我还没有看到很多包含 __main__.py 的软件包(如果有的话),所以我不确定是否人们不使用它们或者我没有使用正确的软件包。事实上,我找不到任何涉及 __main__.py 和 distutils 的博客文章或文章,这一事实表明,它并不是一个特别受欢迎的组合。

Right now I've got a project that has the following layout:

foo/
  __init__.py
  __main__.py
  foo.py

In this case, foo.py is actually the main api file, so developers are meant to do "from foo import foo", but I also wanted to make it so that end users could just run ~$ foo and get an interface.

which, when I do a distutils install, creates /usr/bin/__main__.py because (a) I don't know how to use distutils, [less important] and (b) I am not sure about what is generally considered to be the Right Thing.

As far as I can tell I have three options:

  1. Make distutils smarter, so that setup.py install creates the
    symlink /usr/bin/foo -> $PYTHONLIB/foo/__main__.py. This is my
    immediate intuition, and I could probably figure out how to do it,
    although the things that I'm thinking of doing all feel like hacks
    and I haven't found anybody talking about this.

  2. Rename __main__.py to just foo before distribution, and modify the call to
    distutils' setup to be setup(scripts=['foo'], ...). This is pretty similar to (1), except for when it happens, I think.

  3. Just don't include an interface with a library package. I feel
    like this depends mostly on the size of the library/interface as
    to whether it makes sense.

I haven't seen very many packages that include a __main__.py, if any, so I'm not sure if people just don't use them or I haven't been using the right packages. The fact that I couldn't find any blog posts or articles dealing with __main__.py and distutils suggests to me that it's not a particularly popular combination, though.

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

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

发布评论

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

评论(2

回眸一笑 2024-10-04 15:37:26

调用模块 __main__.py 是一个坏主意,因为该名称具有特殊含义。而是在 __init__ 中使用 主哨兵。 py 并创建一个执行 exec python -m foo 的脚本。

Calling a module __main__.py is a bad idea, since that name has a special meaning. Instead use a main sentinel in __init__.py and create a script that does exec python -m foo.

执笏见 2024-10-04 15:37:26

将 Ignacio Vazquez-Abrams 的答案与一些谷歌搜索相结合,导致我发现 这篇关于使用 _main_.py 的文章,我想我可能会采用如下布局:

foo/
    foo/
        __main__.py
        ...
    scripts/
        foo

where scripts/foo 只是

#!/bin/sh
exec python foo "$@"

这看起来会干净地安装,并且让人们无需安装即可使用我的模块,只需执行python path/to/foo即可。

Combining Ignacio Vazquez-Abrams' answer with some googling that resulted in me finding this article about using _main_.py, I think I'm probably going to go with a layout along the lines of:

foo/
    foo/
        __main__.py
        ...
    scripts/
        foo

where scripts/foo is just

#!/bin/sh
exec python foo "$@"

This seems like it will install cleanly, and let people use my module without installing, just by doing python path/to/foo.

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