Parametrized module import

Oliver Fromme olli at
Thu Jul 8 11:27:18 CEST 2004

Jacek Generowicz <jacek.generowicz at> wrote:
 > I have a module whose behaviour needs to be configurable. The module
 > needs to decide, the first time it is imported, beteween alternative
 > interfaces it presents.
 > Currently, I set some environment variables which select the desired
 > behaviour, and the module inspects those variables to determine the
 > mode in which it should set itself up. I would prefer a pure Python
 > solution, rather than one which depends on external state.
 > Can you recommend any approaches, or warn against the pitfalls of some
 > approaches?

You could create another module called "config" or "cfg"
which contains some global variables.  You import it into
your configurable module as well as into your main program.
Then you can configure the module's behaviour via those
global variables before importing the module.

Something like this:

    #---  File  ---
    foo_interface_mode = 0     # default

    #---  File  ---
    import config
    if config.foo_interface_mode == 0:
        ... this interface
        ... that interface

    #---  File  ---
    import config
    config.foo_interface_mode = 1
    import your_module

Best regards

Oliver Fromme, Konrad-Celtis-Str. 72, 81369 Munich, Germany

``All that we see or seem is just a dream within a dream.''
(E. A. Poe)

More information about the Python-list mailing list