What does the look of a Python application look like?

In most software environments, it is clear how the code extends into several parts and how everything interacts. In Python, I seem to be completely lost.

  • What does the look of a Python application look like?

    I currently have:

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

    I have a script runner with the following content

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

    Can __main__.py be used for this purpose? Is a runner script required?

  • How to import part of the application? (Python 2.6)

    For example, in application_name/__main__.py

     from . import controllers.MainWindow 

How do you plan your applications?

+7
python
source share
1 answer

There are several parts to this question, so I will try to answer them in turn:

1: in fact, this is not so, there are no strict rules, except for those that establish that the directory should be considered as package and so on. Some structures prescribe a directory structure using a script to create forests (a bit like Rails in the Ruby world), but this is just a convenience or convention for the structure. Organize your code and modules so that they make sense logically, like in any other language.

2: What you have there is absolutely normal. Alternatively, you can use the installed script if you use distutils, console_script as part of the .egg installation, or, in extreme cases, just call main.py (or whatever you call) the script directly. For example, console_script is quite common and is used by tools such as nose , for example.

3: There is PEP for this particular topic. In my experience, although you really should prefer absolute imports to relative. To force this behavior you can do:

  from __future__ import absolute_import 
+5
source share

All Articles