How to modularize a Python application -


I have several scripts that use common definitions how can I divide them into multiple files? Apart from this, the application can not be installed in any way in my landscape; It is possible to have an arbitrary number of similarly running versions and it should work without superuser rights. The solutions I have made are:

  • Duplicate code dirty in every script, and perhaps the worst plan.
  • Keep all the scripts and common code in the same directory, use and to import the negative side of this approach to load them It is that I would like to keep my libraries from applications in other directories.
  • Put the usual code in your own directory , type a __ init__ .py that imports all the submodules and finally Use the import to keep the code organized for loading them, but it __init __. There is a little overhead to maintain Py and qualify for names.
  • Library directory to sys.path and import . I think this, but I'm not sure that there is a negligible good code with sys.path .
  • Load execfile (using exec in Python 3). Adds the benefits of the last two approaches: Only one row per module is needed, and I can use a dedicated one. On the other hand, this dragon module avoids disorder and governs global namespace.
  • Write and install modules using distutils . This library installs all dragon scripts and the effects of superuser rights and other applications are required and therefore it is not applicable in my case.

What's the best way? Adding to sys.path (usually using site.addsitedir) is generally common and Especially not frowned on. Of course you want your shared work to be somewhere convenient in the shared content module.

If you are using Python 2.6+ then you can already use a user-level module folder without being included in sys.path or PYTHONPATH. This is a site-package on ~ / .local / lib / python2.6 / UNIX-like - see for more information.


Comments