Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to modularize a Python application
    primarykey
    data
    text
    <p>I've got a number of scripts that use common definitions. How do I split them in multiple files? Furthermore, the application can not be installed in any way in my scenario; it must be possible to have an arbitrary number of versions concurrently running and it must work without superuser rights. Solutions I've come up with are:</p> <ul> <li><b>Duplicate code</b> in every script. Messy, and probably the worst scheme.</li> <li><b>Put all scripts and common code in a single directory,</b> and use <code>from . import</code> to load them. The downside of this approach is that I'd like to put my libraries in other directory than the applications.</li> <li><b>Put common code in its own directory</b>, write a <code>__init__.py</code> that imports all submodules and finally use <code>from . import</code> to load them. Keeps code organized, but it's a little bit of overhead to maintain <code>__init__.py</code> and qualify names.</li> <li><b>Add the library directory to <code>sys.path</code> and <code>import</code>.</b> I tend to this, but I'm not sure whether fiddling with <code>sys.path</code> is nice code.</li> <li><b>Load using <code>execfile</code></b> (<code>exec</code> in Python 3). Combines the advantages of the previous two approaches: Only one line per module needed, and I can use a dedicated. On the other hand, this evades the python module concept and polutes the global namespace.</li> <li><b>Write and install a module using <code>distutils</code>.</b> This installs the library for all python scripts and needs superuser rights and impacts other applications and is hence not applicable in my case.</li> </ul> <p>What is the best method?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload