Using "external" vars on module load time

Discussion in 'Python' started by Marco Aschwanden, Jun 14, 2006.

  1. 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
    Marco Aschwanden, Jun 14, 2006
    #1
    1. Advertising

  2. Marco Aschwanden

    Peter Otten Guest

    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
    Peter Otten, Jun 14, 2006
    #2
    1. Advertising

  3. Marvelous that was the solution I was looking for.

    > I think you should consider a class instead of a module, though.


    What I don't get: Why should I consider using a class?

    Greetings from sunny Switzerland,
    Marco
    Marco Aschwanden, Jun 14, 2006
    #3
  4. Marco Aschwanden schrieb:
    > Marvelous that was the solution I was looking for.
    >
    >> I think you should consider a class instead of a module, though.

    >
    > What I don't get: Why should I consider using a class?


    Because sharing state in such a way that several functions need to
    access some later bound globals cries for a class that gets that very
    state passed on instantiation.

    Diez
    Diez B. Roggisch, Jun 14, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon

    app vars and cache vars

    Jon, Dec 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    377
  2. Replies:
    1
    Views:
    308
    Diez B. Roggisch
    Oct 29, 2006
  3. flamesrock
    Replies:
    8
    Views:
    434
    Hendrik van Rooyen
    Nov 24, 2006
  4. Linuxguy123
    Replies:
    7
    Views:
    662
    Paddy O'Loughlin
    Feb 20, 2009
  5. caccolangrifata
    Replies:
    18
    Views:
    382
    Chris Torek
    Jul 22, 2011
Loading...

Share This Page