import from string?

Mike mgould at orchestream.com
Tue Jan 29 07:25:13 EST 2002


Hi,
I'm developing a system that uses python as an embedded scripting engine.

The scripts are written as python modules that must contain a class
providing a particular interface.

I'd like all the scripts to be imported as modules to prevent namespace
clashes and so that the scopeing behaviour is as the script authors expect.
The problem is that I can't see how to import python code held in a string
instead of a file.

I've looked at using the __import__ function but in all cases this looks for
a built-in module or a file.
I've also tried using exec and specifying the local and global scopes
explicitly but this requires the namespaces to be created beforehand.  Using
the class statement to create namespaces is probably not the way forward and
providing ordinary dictionary objects doesn't seem to work properly.

Should I fall back to the C API and call PyImport_ExecCodeModule or
something?  If so I'll probably have to write an extension that allows this
to be called from python.

Is this possible or do you think that I'm going about things in the wrong
way?

thanks

Mike





More information about the Python-list mailing list