Using "external" vars on module load time

Marco Aschwanden PPNTWIMBXFFC at spammotel.com
Wed Jun 14 05:46:35 EDT 2006


Hi

I have a script that looks for modules and tries to load them. After  
loading I attach the "server"-API function to it.

module = __import__(module_name, globals(), locals(), [])
module.server = server.server_api

Now, some modules need the "server" functionality on load/init. I tried to  
modify the globals():

my_globals = globals().copy()
my_globals['server'] = server.server_api
module = __import__(module_name, my_globals, locals(), [])
module.server = server.server_api

This did not work as intended the module to be imported did not get the  
"server" from my_globals. The solution I have, is to add __init_module__()  
method that is called after the module was imported:

module = __import__(module_name, globals(), locals(), [])
module.server = server.server_api
if module.__dict__.has_key('__init_module__'):
	module.__init_module__()

This is not really satisfying. I would like:

Load a module and hand in already the server_api. How can I achieve this?

Thanks for any suggestions,
Marco




More information about the Python-list mailing list