分发 python 应用程序

发布于 2024-08-17 11:40:54 字数 460 浏览 5 评论 0原文

我有一个简单的 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 技术交流群。

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

发布评论

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

评论(1

旧时光的容颜 2024-08-24 11:40:54

setuptools 将你的包安装在一个可以从 python 访问的位置,即你可以导入它:

import project

当你执行相对导入而不是绝对导入时,就会出现问题。如果您的 main.py 导入 config.py 它会起作用,因为它们位于同一目录中。当您将 main.py 移动到 /usr/bin 等其他位置或 PATH 环境变量中存在的其他位置时,python 会尝试导入 < code>config.py 来自 sys.path 而不是来自您的包目录。解决方案是使用绝对导入:

from project import config

现在 main.py 是“可移动的”。

我更喜欢的另一个解决方案是使用 setuptools 提供的自动脚本创建。

不要将代码放在

if __name__ == "__main__":
    # here all your beautiful code

语句中,而是将代码放在函数中(main 可能是一个好名字):

def main():
    # put your code here

if __name__ == "__main__":    # not needed, just in case...
    main()

现在修改您的 setup.py

setup(
    # ...
    entry_points = {
        "console_scripts": [
            # modify script_name with the name you want use from shell
            # $ script_name [params]
            "script_name = project.main:main",
        ],
    }
)

仅此而已。安装后,setuptools 将创建一个包装脚本,该脚本可从 shell 调用并调用您的 main 函数。现在 main.py 可以位于您的项目目录中,您不再需要将其移动到 bin/ 目录中。请注意,setuptools 会自动将此脚本放入相对于安装前缀的 bin/ 目录中。

es.

python setup.py install --prefix ~/.local

安装您的项目包

~/.local/lib/python<version>/site-packages/<package_name>

和脚本,

~/.local/bin/<script_name>

确保您的 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:

import project

the problem raise when you do relative imports instead of absolute imports. if your main.py imports config.py it works because they live in the same directory. when you move your main.py to another location like /usr/bin or another location present in PATH environment variable, python try to import config.py from sys.path and not from your package dir. the solution is to use absolute import:

from project import config

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

if __name__ == "__main__":
    # here all your beautiful code

statement, put your code in a function (main could be a good name):

def main():
    # put your code here

if __name__ == "__main__":    # not needed, just in case...
    main()

now modify your setup.py:

setup(
    # ...
    entry_points = {
        "console_scripts": [
            # modify script_name with the name you want use from shell
            # $ script_name [params]
            "script_name = project.main:main",
        ],
    }
)

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 a bin/ directory. note that setuptools automatically puts this script in the bin/ directory relative to the installation prefix.

es.

python setup.py install --prefix ~/.local

install your project package in

~/.local/lib/python<version>/site-packages/<package_name>

and your script in

~/.local/bin/<script_name>

so be sure that ~/.local/bin is present in your PATH env.

more info at: http://peak.telecommunity.com/DevCenter/setuptools#automatic-script-creation

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