Load Python Scripts from Memory

Dave Brueck dave at pythonapocrypha.com
Wed Nov 12 22:08:17 CET 2003


Dean Ellis wrote:
> Thanks for all the suggestions guys.
>
> I have to admit writing my own import function sounds a bit daunting.
> I think it might help if a clarify what I need to do.
>
> Basically, my application is written in C/Delphi (what ever) and
> provides a Host API to python to allow scripts to modify the
> application objects. What we would like to support is a form of
> automation, i.e we have a pre-compiled python script on our trusted
> server, the application loads on the client machine detects there is a
> new script and downloads it into memory, it then runs that script in
> python. This could be done by saving the compiled script to a file
> before running but I would prefer not to.
>
> I did have a play with the PyCodeObject but I couldn't get it to work.

I think Alex's and Paul's answers give you the same info that's in the code
below, but those solutions may be more general than you need. Hooking into the
normal import mechanism lets your custom imports be more transparent to the
rest of your application, but if you don't need that then you can strip down
the code they gave you to just something like this:

import new
module = new.module('ModuleName')
sourceCode = ObtainSourceCodeFromSomewhere()
exec sourceCode in module.__dict__
sys.modules['ModuleName'] = module

(error handling and security is left up to you)

Note that if Windows is involved anywhere in the process then you probably want
to do something like:

sourceCode = ObtainSourceCodeFromSomewhere().replace('\r', '')

(because Windows does \r\n for line endings, and IIRC exec doesn't like those)

-Dave

P.S. At my company we're doing something like what you describe and it has been
a _huge_ win for us. As long as you are careful about security it enables
functionality that statically-written-and-compiled applications have a tough
time competing with.






More information about the Python-list mailing list