Scope of InteractiveInterpreter?

Michael Hudson mwh at python.net
Thu Aug 21 08:19:42 EDT 2003


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

-- 
  I've reinvented the idea of variables and types as in a
  programming language, something I do on every project.
                                          -- Greg Ward, September 1998




More information about the Python-list mailing list