构建:使用“包”作为切入点?
我的项目中有一个名为 ./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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,python 通过 sys.path 变量通过 python 路径查找模块和包。当
buildout
在bin/
目录中创建脚本时,它将添加到sys.path
变量,列出给定部分的所有 Eggs 。为了让 python 找到您的
./bar.py
模块或./foo
包,当前目录也需要是 python 路径的一部分。我发现非常令人惊讶的是,为bin/bar
找到了bar.py
模块,而./foo
> 找不到包;显然,两个部分的 python 路径是不同的。您可以查看生成的bin/bar
和bin/foo
脚本的顶部,以查看已将哪些路径添加到sys.path
中。在任何情况下,您都可以使用
zc.recipe.egg
部分中的extra-paths
选项手动将其他路径添加到sys.path
。那里列出的任何路径最终都会出现在您生成的脚本中。只需将其设置为${buildout:directory}
就足够了:Normally, python finds modules and packages via the python path, via the
sys.path
variable. Whenbuildout
creates scripts in thebin/
directory, it'll add to thesys.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 abar.py
module is found forbin/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 generatedbin/bar
andbin/foo
scripts to see what paths have been added tosys.path
.In any case, you can manually add additional paths to
sys.path
with theextra-paths
option in thezc.recipe.egg
part. Any path listed there will end up in your generated script. Simply setting this to${buildout:directory}
is enough: