views:

262

answers:

1

In most programming environments it's clear how the code is distributed into several parts and how everything interacts. In Python I seem to be completely lost.

  • How should the layout of a Python application look?

    Currently I have:

    setup.py
    application_name/
        __main__.py
        __init__.py
        views/
        controllers/
        model/
        resources/   <- images, videos, ...
    
  • How does one execute the application?

    I've got a runner script with the following content

    #!/usr/bin/env python -m "application_name"
    

    Should one even use __main__.py for this purpose? Is a runner script necessary?

  • How should one import parts of the application? (Python 2.6)

    For example in application_name/__main__.py

    from . import controllers.MainWindow
    

How do you layout your applications?

+4  A: 

There are several parts to this question so I'll try to answer them in turn:

1: Its really up to you, there are no hard-and-fast rules beyond those for establishing that a directory should be treated as a package and so on. Some frameworks will prescribe a directory structure using a script to generate scaffolding (a bit like Rails does in the Ruby world) but this is purely a convenience or convention of the given framework. Organise your code and modules so they make sense logically as you would in any other language.

2: What you have there is absolutely fine. Alternatively you can use an installed script if you are using distutils, a console_script as part of a .egg install, or as a last resort just call the main.py (or whatever you name it) script directly. The console_script is quite common though and is used by tools such as the nose testing framework for example.

3: There is a PEP for this specific topic. In my experience though you should really prefer absolute imports to relative ones. To force this behaviour you can do:

 from __future__ import absolute_import
jkp
What about explicit relative imports? `from . import subpackage`? I'm a bit concerned that absolute imports break when I move some packages around or rename them.
Georg
I hate not having clear rules for this. I'm constantly unsure if I'm doing it right. It's one of those things every programmer has to figure out alone and spends a lot of time doing exactly that. It would have saved a lot of time if there were some guidelines. (At least for me.)
Georg
gs: Python is pretty prescriptive in a lot of places, for example PEP8 which specifies the correct style to use! (C programmers HATE this...) The PEP I linked to tries to clarify the situation on imports, but as far as project layout goes thats hard to legislate. Within a given framework yes, but otherwise its open to debate. Best thing is to read a lot of code and see what you like.
jkp
PS: I didn't even know about `__main.py__` btw (not something I've used). Every day's a schoolday... :)
jkp
@jkp: `__main__.py` is new in Python 2.6 or 2.5. I'm using `PyQt` which doesn't force a structure. I think I'll just finish my first project in PyQt with the layout I've got and then go studying other projects. Thanks.
Georg