Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the layout of a Python application look? [closed]

Tags:

python

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?

like image 984
Georg Schölly Avatar asked Nov 23 '09 07:11

Georg Schölly


1 Answers

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
like image 158
jkp Avatar answered Nov 03 '22 17:11

jkp