使用 2 个不同的应用程序构建一个 python 项目

发布于 2024-12-07 04:07:09 字数 414 浏览 0 评论 0 原文

我计划用 python/GTK 编写一个访客信息亭。该项目将有两个应用程序,即信息亭的前端和连接到 MySQL 数据库的管理后端。

我希望两个应用程序都有共同的代码。 我正在考虑像这样构建项目:

project.common - 用于公共代码

project.frontend - 用于前端。

project.backend - 用于后端。

所以:

project/
    common
    frontend
    backend

我将遇到的问题是每个应用程序都有自己的数据文件,并且我希望将应用程序单独打包。前端将仅在 Linux 上运行,后端将在 Windows(cx_freeze) 和 Linux 上运行。

谁能给我任何建议吗?

I am planning to write a visitors' kiosk in python/GTK. The project will have two applications, the frontend for the kiosk and the backend for management linked to a MySQL DB.

I want both applications to have common code.
I was thinking of structuring the project like so:

project.common - for common code

project.frontend - for the frontend.

project.backend - for the backend.

So:

project/
    common
    frontend
    backend

The problem that I'm going to have is that each application will have its own data_files and I will want the applications packaged separately. The frontend will just run on Linux and the backend will be on Windows(cx_freeze) and Linux.

Can anyone give me any advice?

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

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

发布评论

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

评论(1

颜漓半夏 2024-12-14 04:07:09

您的 PYTHONPATH 中可以有多个目录(即 common/projectfrontend/projectbackend/project 目录) code> 具有重叠的层次结构(即每个都有 project 顶级包),但默认情况下 Python 不会对此感到满意,因为它使用它找到的 first 匹配目录,并且不像许多其他语言那样搜索所有这些(PHP、Java 等),因此您会遇到导入错误。

但是,通过将其放入每个 project/ 目录的 __init__.py 中,您可以有效地告诉 Python 继续查找,并且这并不是查找此包的代码的唯一位置:

import pkgutil
__path__ = pkgutil.extend_path(__path__, __name__)

许多 Python 爱好者会因为你如此荒谬的反常行为而回避和嘲笑你,并宣称这种努力是愚蠢的,并可能对你的灵长类祖先提出令人不快的建议。他们会说:“命名空间包是一个糟糕的主意”。准备捍卫你的决定。如果没有附带注释解释它们的作用,您难道不敢将这两行添加到您的源代码树中吗!

此页面对其中一些概念有很好的解释:
说明: http://www.doughellmann.com/PyMOTW/pkgutil/

如果您按照此操作方法,您可以使用 common+frontend 制作一个发行版,并使用 common+backend 制作另一个发行版。

You can have multiple directories (i.e. common/project, frontend/project, and backend/project directories) in your PYTHONPATH with overlapping hierarchies (i.e. each has project top level package), but by default Python will not be happy with this, because it uses the first matching directory it finds and does not search all of them as many other languages do (PHP, Java, etc.), so you will get import errors.

However by putting this in each project/ directory's __init__.py, you are effectively telling Python to keep looking and that this isn't the only place to find code for this package:

import pkgutil
__path__ = pkgutil.extend_path(__path__, __name__)

Many Pythonistas will shun and mock you for such ridiculous an aberration and declare the foolishness of this endeavor and possibly make unpleasant suggestions about your primate ancestry. "Namespace packages are a terrible idea" they'll say. Prepare to defend your decision. And don't you DARE add those two lines to your source tree without accompanying comments explaining what they do!

This page has a good explanation of some of these concepts:
Explanation: http://www.doughellmann.com/PyMOTW/pkgutil/

If you follow this approach, you can make a distribution with common+frontend and another with common+backend.

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