interesting Namespace problem

Markus Gritsch gritsch at iue.tuwien.ac.at
Thu Apr 19 05:28:42 EDT 2001


Hi!

Recently I tried the following code snippet which works ok:

def do_stuff(stuff):
    exec stuff

def import_module():
    global os
    import os
    do_stuff('print os.sep')

import_module()

Making os a global name is not very elegant, but I found no other way to
make this name accessible in do_stuff().  Now I want to pass the module's
name to import_module():

def do_stuff(stuff):
    exec stuff

def import_module(module):
    exec 'global ' + module
    exec 'import ' + module
    do_stuff('print os.sep')

import_module('os')

And here the problem arises: The name os is not known in do_stuff().  Is
there a way to make the namespace of import_module() available in
do_stuff()?

Kind regards, Markus



More information about the Python-list mailing list