也有接口模块的 python 库的一般约定?
现在我有一个具有以下布局的项目:
foo/
__init__.py
__main__.py
foo.py
在这种情况下,foo.py
实际上是主api文件,因此开发人员应该执行“from foo import foo”,但我还想让最终用户只需运行 ~$ foo
并获得一个界面。
当我安装 distutils 时,会创建 /usr/bin/__main__.py
因为 (a) 我不知道如何使用 distutils,[不太重要] 并且 (b) 我不知道确定什么通常被认为是正确的事情。
据我所知,我有三个选择:
使 distutils 更智能,以便
setup.py install
创建 符号链接/usr/bin/foo -> $PYTHONLIB/foo/__main__.py
。这是我的 直接的直觉,我也许能弄清楚该怎么做, 虽然我想做的事情都感觉像是黑客 我还没有发现有人谈论这个。在分发之前将
__main__.py
重命名为foo
,并将调用修改为 distutils 的设置为setup(scripts=['foo'], ...)
。这与 (1) 非常相似,除了它发生的时间,我认为。只是不要在库包中包含接口。我觉得 像这样主要取决于库/接口的大小 看看它是否有意义。
我还没有看到很多包含 __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:
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.Rename
__main__.py
to justfoo
before distribution, and modify the call to
distutils' setup to besetup(scripts=['foo'], ...)
. This is pretty similar to (1), except for when it happens, I think.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调用模块
__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 doesexec python -m foo
.将 Ignacio Vazquez-Abrams 的答案与一些谷歌搜索相结合,导致我发现 这篇关于使用 _main_.py 的文章,我想我可能会采用如下布局:
where
scripts/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:
where
scripts/foo
is justThis seems like it will install cleanly, and let people use my module without installing, just by doing
python path/to/foo
.