Using "external" vars on module load time
Peter Otten
__peter__ at web.de
Wed Jun 14 08:35:22 EDT 2006
Marco Aschwanden wrote:
> Load a module and hand in already the server_api. How can I achieve this?
Using execfile() is the simplest approach:
import imp
import sys
def import_preloaded(name, path=None, variables={}):
if path is None:
file, path, description = imp.find_module(name)
module = imp.new_module(name)
module.__dict__.update(variables)
execfile(path, module.__dict__)
sys.modules[name] = module
return module
if __name__ == "__main__":
demo = import_preloaded("demo", variables=dict(whatever=42))
I think you should consider a class instead of a module, though.
Peter
More information about the Python-list
mailing list