passing globals to imported module

Peter Otten __peter__ at
Mon Aug 16 22:21:41 CEST 2004

James Tauber wrote:

> He has a script, with a global. He wants to import and
> needs that global available in

He should _not_ consider the following hack:

>>> import new, sys
>>> bar = new.module("bar")
>>> bar.my_global = "some value"
>>> sys.modules["bar"] = bar
>>> execfile("", bar.__dict__)
['__builtins__', '__name__', '__doc__', 'my_global']
some value

$ cat
print __name__
print globals().keys()

def demo():
    print my_global



More information about the Python-list mailing list