Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Isolated environment for execfile

Reply
Thread Tools

Isolated environment for execfile

 
 
Igor Kaplan
Guest
Posts: n/a
 
      10-01-2008
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.


 
Reply With Quote
 
 
 
 
Lie Ryan
Guest
Posts: n/a
 
      10-01-2008
On Wed, 01 Oct 2008 11:11:29 +0000, Igor Kaplan wrote:

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

 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      10-02-2008
En Wed, 01 Oct 2008 08:11:29 -0300, Igor Kaplan
<(E-Mail Removed) > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting up an Isolated .NET Team Environment =?Utf-8?B?UmFscGg=?= ASP .Net 2 04-08-2005 02:17 PM
Article: What is Isolated Storage ? (.Net FrameWork Tools Series) Namratha Shah \(Nasha\) ASP .Net 0 11-17-2004 12:39 AM
Vss and VS.NET integration in Non-Isolated Development mode =?Utf-8?B?R2Fs?= ASP .Net 0 06-17-2004 08:15 AM
Isolated Development model problems =?Utf-8?B?Sm9obiBUYWNrZQ==?= ASP .Net 3 03-01-2004 11:54 PM
Install AD, DNS, DHCP on an isolated stand alone pc? Javier GP MCSE 5 09-30-2003 12:42 AM



Advertisments