I try to avoid using any of the
__xxxx__() functions if possible
(considering this a good
programming style).
This is never good style, at least in the case of exec. exec is evil.
What works (beware that the below code is nevertheless untested and might
contain little warts) and is the "usual" and clean way to go:
### libinfo/__init__.py
# Empty.
### libinfo/Module1.py
def libinfo():
return "I am module1."
CFLAGS = ["-DMODULE1"]
### libinfo/Module2.py
def libinfo():
return "I am module2."
CFLAGS = ["-DMODULE2"]
### Importer.py
modules = {}
CFLAGS = []
def load_modules(to_load=["module1","module2"]):
global modules, CFLAGS
for mod in to_load:
try:
modules[mod] = getattr(__import__("libinfo.%s" % mod),mod)
except ImportError:
print "Could not load %s." % mod
continue
print "Module: %s (%r)." % (mod,modules[mod])
print "Modules libinfo: %r." % modules[mod].libinfo()
CFLAGS += modules[mod].CFLAGS
print "Total CFLAGS: %s." % CFLAGS
if __name__ == "__main__":
load_modules()
print "Module container: %s." % modules
### End Importer.py
HTH!
--
--- Heiko.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)
iD8DBQBCLCgXf0bpgh6uVAMRAhCUAJ95kbz6bMWAV7j42hxrh1nYEvj4lgCfZoD3
p0E0cdgp4io2eyGfL6u2lho=
=wYq0
-----END PGP SIGNATURE-----