Isolated environment for execfile
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Thu Oct 2 01:20:59 EDT 2008
En Wed, 01 Oct 2008 08:11:29 -0300, Igor Kaplan
<igor_kaplan_remove_the_rest_of_line at hotmail.com> escribió:
> I got quite unusual problem and all my searches to find the answer on
> my
> own were not successful.
> Here is the scenario:
> I have the python program, let's call it script1.py, this program
> needs to
> execute another python script, let's call it script2.py.
> In script1.py I have the statement:
> execfile('script2.py')
> Everything is fine beside one thing. The script2.py is pretty big
> python
> program which does a lot of things and also while runs, it modifies many
> variables and module members, such for example as sys.path. So when
> script2.py exits all changes which it does are visible in my main
> program,
> script1.py. Even more, I need to execute script2.py in loop, several
> times
> during script1.py session. And all changes, which script2.py does just
> accumulate.
>
> I wander, is there any way to execute script2.py in it's own
> environment,
> so when script2.py exits, all modifications, which it is done in global
> modules are gone?
If you want a true isolated execution, start a new Python process:
subprocess.call([sys.executable, "script2.py"])
But I feel this is not the right thing to do - instead of *executing* many
times script2.py, maybe you have to *import* some functions or classes
from it, and then use them.
--
Gabriel Genellina
More information about the Python-list
mailing list