Scope of InteractiveInterpreter?

Kris Caselden google at hanger.snowbird.net
Sat Aug 23 04:31:46 EDT 2003


What I'm trying to do is dynamically add code to a running program.
Essentially, file1.py could execute file2.py, then the code in both
files could execute and read each others data both ways. It seems like
execfile almost works, but the communication only happens one way,
from file1 to file2. Is there any way to get around this?

Michael Hudson <mwh at python.net> wrote in message news:<7h3vfsr2oj4.fsf at pc150.maths.bris.ac.uk>...
> google at hanger.snowbird.net (Kris Caselden) writes:
> 
> > I'm using the InteractiveInterpreter to run some source, but the data
> > generated doesn't seem to be available in the scope of the parent.
> > 
> > For instance, I tried
> > 
> > lines = infile.open('data.py','r').readlines()
> > ii = InteractiveInterpreter({'__name__': '__main__', '__doc__': None})
> > for line in lines:
> >     ii.runsource(line)
> > 
> > but the data objects in data.py don't seem to exist when I try and
> > manipulate them later in the script. Is what I'm trying to do
> > possible? Am I on the right track?
> 
> I don't know what you're trying to acheive.  It *sounds* like you're
> hunting for a complicated version of execfile.
> 
> Try this:
> 
> d = {}
> execfile('data.py', d, d)
> 
> then the objects you're looking for are in the dictionary d.  If you
> really want a module you can then do
> 
> data = new.module('data', d)
> 
> or maybe just use the standard (but obscure) imp module to do it all
> in one step.
> 
> HTH,
> mwh




More information about the Python-list mailing list