构建:使用“包”作为切入点?

发布于 2024-11-05 04:32:33 字数 499 浏览 0 评论 0原文

我的项目中有一个名为 ./foo 的目录,其中包含一个 __init__.py 文件,其中包含一个名为 main() 的方法。

我想使用 buildout 创建一个可执行文件,它将执行 main() 方法(例如:./bin/foo)。我使用以下 buildout.cfg 部分在目录结构的“顶层”实现了类似的效果:

[bar]
recipe = zc.recipe.egg
eggs = ${buildout:eggs}
entry-points = bar=bar:main

这对于我的 ./bar.py 文件效果很好,创建可执行 ./bin/bar 文件。我似乎无法让它适用于 ./foo/__init__.py 文件。

我怎样才能实现上述目标?

I have a directory named ./foo in my project which contains an __init__.py file, which contains a method named main().

I'd like to use buildout to create an executable which will execute the main() method (eg: ./bin/foo). I've achieved something similar at the "top-level" of my directory structure using the following buildout.cfg section:

[bar]
recipe = zc.recipe.egg
eggs = ${buildout:eggs}
entry-points = bar=bar:main

This works fine for my ./bar.py file, creating an executable ./bin/bar file. I just can't seem to get it to work for the ./foo/__init__.py file.

How could I achieve the above?

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

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

发布评论

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

评论(1

妳是的陽光 2024-11-12 04:32:33

通常,python 通过 sys.path 变量通过 python 路径查找模块和包。当 buildoutbin/ 目录中创建脚本时,它将添加到 sys.path 变量,列出给定部分的所有 Eggs 。

为了让 python 找到您的 ./bar.py 模块或 ./foo 包,当前目录也需要是 python 路径的一部分。我发现非常令人惊讶的是,为 bin/bar 找到了 bar.py 模块,而 ./foo > 找不到包;显然,两个部分的 python 路径是不同的。您可以查看生成的 bin/barbin/foo 脚本的顶部,以查看已将哪些路径添加到 sys.path 中。

在任何情况下,您都可以使用 zc.recipe.egg 部分中的 extra-paths 选项手动将其他路径添加到 sys.path。那里列出的任何路径最终都会出现在您生成的脚本中。只需将其设置为 ${buildout:directory} 就足够了:

[foo]
recipe = zc.recipe.egg
eggs = ${buildout:eggs}
extra-paths = ${buildout:directory}
entry-points = foo=foo:main

Normally, python finds modules and packages via the python path, via the sys.path variable. When buildout creates scripts in the bin/ directory, it'll add to the sys.path variable, listing all the eggs for the given part.

In order for python to find your ./bar.py module, or your ./foo package, the current directory needs to part of python path too. I find it very surprising that a bar.py module is found for bin/bar, while the ./foo package is not found; clearly the python paths for both parts are different. You can look at the top of the generated bin/bar and bin/foo scripts to see what paths have been added to sys.path.

In any case, you can manually add additional paths to sys.path with the extra-paths option in the zc.recipe.egg part. Any path listed there will end up in your generated script. Simply setting this to ${buildout:directory} is enough:

[foo]
recipe = zc.recipe.egg
eggs = ${buildout:eggs}
extra-paths = ${buildout:directory}
entry-points = foo=foo:main
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文