passing globals to imported module

Peter Otten __peter__ at web.de
Mon Aug 16 16:21:41 EDT 2004


James Tauber wrote:

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

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.py", bar.__dict__)
bar
['__builtins__', '__name__', '__doc__', 'my_global']
some value
>>>

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

def demo():
    print my_global

demo()

Peter




More information about the Python-list mailing list