passing globals to imported module

James Tauber jtauber at jtauber.com
Mon Aug 16 21:00:05 CEST 2004


Had a question from a colleague that I embarrassingly couldn't answer.

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

The following obviously doesn't work:

# foo.py
my_global = "hello"
print globals().keys()
import bar


# bar.py
print globals().keys()


and results in:

['__builtins__', '__name__', '__doc__', 'my_global']
['__builtins__', '__name__', '__file__', '__doc__']


I'm not sure how to reimplement __import__ to make the global available
to the imported module.

Any suggestions?

James
-- 
  James Tauber               http://jtauber.com/
  journeyman of some    http://jtauber.com/blog/




More information about the Python-list mailing list