分发 python 应用程序
我有一个简单的 python 应用程序,其中我的目录结构如下:
- project/
- main.py
- config.py
- 插件/
- 插件1
- 插件2
- ...
Config.py 仅加载配置文件,它本身不包含任何配置信息。
我现在想分发这个程序,我想我应该使用 setuptools 来完成它。用户预计使用的文件是 main.py,因此其中一个文件明确进入 /usr/bin,其余文件进入 /usr/share/project。
但有一个问题:我需要以某种方式告诉 main.py 在共享目录中查找 config.py。但我无法确定共享目录到底在哪里,因为这取决于 setuptools,对吗?
分发基于 Python 的应用程序时的最佳实践是什么?
I have a simple python application where my directory structure is as follows:
- project/
- main.py
- config.py
- plugins/
- plugin1
- plugin2
- ...
Config.py only loads configuration files, it does not contain any configuration info in itself.
I now want to distribute this program, and I thought I'd use setuptools to do it. The file users are expected to use is main.py, so that one clearly goes into /usr/bin and the rest of the files go into /usr/share/project.
But there's one problem: I would somehow need to tell main.py to look for config.py in the share directory. But I can't really be sure where exactly the share directory is since that's up to setuptools, right?
What's the best practice when distributing Python-based applications?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setuptools 将你的包安装在一个可以从 python 访问的位置,即你可以导入它:
当你执行相对导入而不是绝对导入时,就会出现问题。如果您的
main.py
导入config.py
它会起作用,因为它们位于同一目录中。当您将main.py
移动到/usr/bin
等其他位置或 PATH 环境变量中存在的其他位置时,python 会尝试导入 < code>config.py 来自sys.path
而不是来自您的包目录。解决方案是使用绝对导入:现在 main.py 是“可移动的”。
我更喜欢的另一个解决方案是使用 setuptools 提供的自动脚本创建。
不要将代码放在
语句中,而是将代码放在函数中(main 可能是一个好名字):
现在修改您的
setup.py
:仅此而已。安装后,setuptools 将创建一个包装脚本,该脚本可从 shell 调用并调用您的 main 函数。现在
main.py
可以位于您的项目目录中,您不再需要将其移动到bin/
目录中。请注意,setuptools 会自动将此脚本放入相对于安装前缀的bin/
目录中。es.
安装您的项目包
和脚本,
确保您的 PATH 环境中存在
~/.local/bin
。更多信息请访问:http://peak.telecommunity.com/DevCenter/setuptools#自动脚本创建
setuptools install your package in a location which is reachable from python i.e. you can import it:
the problem raise when you do relative imports instead of absolute imports. if your
main.py
importsconfig.py
it works because they live in the same directory. when you move yourmain.py
to another location like/usr/bin
or another location present in PATH environment variable, python try to importconfig.py
fromsys.path
and not from your package dir. the solution is to use absolute import:now
main.py
is "movable".another solution, which i prefer, is using automatic script creation offered by setuptools.
instead of having your code in a
statement, put your code in a function (main could be a good name):
now modify your
setup.py
:that's all. after an install setuptools will create a wrapper script which is callable from shell and that calls your main function. now
main.py
can live in your project directory and you don't need anymore to move it in abin/
directory. note that setuptools automatically puts this script in thebin/
directory relative to the installation prefix.es.
install your project package in
and your script in
so be sure that
~/.local/bin
is present in your PATH env.more info at: http://peak.telecommunity.com/DevCenter/setuptools#automatic-script-creation