Isolated environment for execfile

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.
 
L

Lie Ryan

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. (snip)
Thanks in advance.

Igor.

I smelled a really strong sign of bad code.

1. In python, functional style programming is very much preferred. In
short, functional style programming requires that: a function never makes
a side-effect (python doesn't enforce this[1] as python is not a pure
functional language).

2. In any programming language, the use of global variable must be
minimized, and modifying global is even more frowned upon.

Aside:
If you need an isolated environment, it is probably much better if you
use class. Each instance of a class lives separately, independent to each
other.

e.g.:
class Test(object):
def func(self, n):
self.n = n
a = Test()
b = Test()
a.func(10)
b.func(20)
print a.n # 10
print b.n # 20

[1] in fact, many built-in standard library do use side-effects, and OOP-
style programming (more appropriately Java-style OOP) relies heavily on
side-effect.
 
G

Gabriel Genellina

En Wed, 01 Oct 2008 08:11:29 -0300, Igor Kaplan
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top