passing globals to imported module

Peter Otten __peter__ at web.de
Mon Aug 16 22:21:41 CEST 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