Invoking Python from Python

Martin Miller ggrp1.20.martineau at dfgh.net
Tue Nov 8 15:35:04 EST 2005


John Henry wrote:
> Hi all,
>
> I have a need to create a Python script on the fly from another Python
> program and then execute the script so created.  Do I need to invoke
> Python through os.spawnl or is there a better way?

When doing something similar to this I used the built-in 'execfile()'
function to execute the code created (and retrieve its results).

Cameron Laird wrote:
>Once I had to maintain lisp code which stored its data in lisp code, too
>(incl. conditions and loops). It was a nightmare.

This is exactly what my Python program does, but I've found it to be a
very powerful and useful technique while remaining relatively easy to
maintain (the generated code doesn't contain any conditionals or loops,
however).  Another nice by-product is that the data stored this way is
portable to different platforms.

-Martin




More information about the Python-list mailing list