I
Igor Kaplan
Hello python gurus.
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?
Ideally I would love to have the following.
script1.py:
import sys
i = 10
print len(sys.path) # displays 5 for example
execfile('script2.py')
print i # still displays 10
print len(sys.path) # still displays 5
script2.py:
import sys
i += 10
sys.path.insert(0, '\\my folder')
Sorry for such long and probably confusing message. As you probably see, I
am not really strong python programmer and don't know advanced techniques. I
have spent several days trying to look for the solution, the problem is, I
even don't know what to look for. All references to execfile more talk about
saving the environment instead of isolating it.
Would so much appreciate any help.
Thanks in advance.
Igor.
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?
Ideally I would love to have the following.
script1.py:
import sys
i = 10
print len(sys.path) # displays 5 for example
execfile('script2.py')
print i # still displays 10
print len(sys.path) # still displays 5
script2.py:
import sys
i += 10
sys.path.insert(0, '\\my folder')
Sorry for such long and probably confusing message. As you probably see, I
am not really strong python programmer and don't know advanced techniques. I
have spent several days trying to look for the solution, the problem is, I
even don't know what to look for. All references to execfile more talk about
saving the environment instead of isolating it.
Would so much appreciate any help.
Thanks in advance.
Igor.