Parametrized module import

george young gry at ll.mit.edu
Thu Jul 8 10:01:21 EDT 2004


On 08 Jul 2004 09:43:31 +0200
Jacek Generowicz <jacek.generowicz at cern.ch> threw this fish to the penguins:

> 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 might look at the way pygtk does it.  I haven't peeked inside, but the 
api is:

import pygtk
pygtk.require('2.0') # or '1.2' or some other version
import gtk           # uses state set by require() to load the appropriate version

The pygtk.py file looks quite reusable with hardly any change...

See: http://www.pygtk.org


-- George
-- 
"Are the gods not just?"  "Oh no, child.
What would become of us if they were?" (CSL)



More information about the Python-list mailing list