我计划用 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?
发布评论
评论(1)
您的
PYTHONPATH
中可以有多个目录(即common/project
、frontend/project
和backend/project
目录) code> 具有重叠的层次结构(即每个都有project
顶级包),但默认情况下 Python 不会对此感到满意,因为它使用它找到的 first 匹配目录,并且不像许多其他语言那样搜索所有这些(PHP、Java 等),因此您会遇到导入错误。但是,通过将其放入每个
project/
目录的__init__.py
中,您可以有效地告诉 Python 继续查找,并且这并不是查找此包的代码的唯一位置:许多 Python 爱好者会因为你如此荒谬的反常行为而回避和嘲笑你,并宣称这种努力是愚蠢的,并可能对你的灵长类祖先提出令人不快的建议。他们会说:“命名空间包是一个糟糕的主意”。准备捍卫你的决定。如果没有附带注释解释它们的作用,您难道不敢将这两行添加到您的源代码树中吗!
此页面对其中一些概念有很好的解释:
说明: http://www.doughellmann.com/PyMOTW/pkgutil/
如果您按照此操作方法,您可以使用
common
+frontend
制作一个发行版,并使用common
+backend
制作另一个发行版。You can have multiple directories (i.e.
common/project
,frontend/project
, andbackend/project
directories) in yourPYTHONPATH
with overlapping hierarchies (i.e. each hasproject
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: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 withcommon
+backend
.