开发者

Structure a python project with 2 different applications

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜