473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Isolated environment for execfile

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.
Oct 1 '08 #1
2 1610
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.

Oct 1 '08 #2
En Wed, 01 Oct 2008 08:11:29 -0300, Igor Kaplan
<ig*********************************@hotmail.comes cribió:
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

Oct 2 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
1
by: Bo Jacobsen | last post by:
I have a number of files compiled to bytecode using py_compile.compile(). The .pyc files can be invoked by python directly ($python file.pyc), but "loading" them by execfile just throws an...
1
by: malcolm | last post by:
Hello, We use several user controls and derived custom controls. Some of which actually hit the database at design time to show data (such as filling a list box, etc...) Our c# client server...
2
by: Ralph | last post by:
Hi, I've done a bit of reading on setting up an Isolated .NET environment for each of my developers. We all run Windows XP Pro, IIS and have local instances of SQL Server for development. I...
3
by: Wm. Scott Miller | last post by:
We have been looking at how to develop in a team environment where our servers are all Windows Server 2003 (IIS 6.0) and all our development machines are Windows XP (IIS 5.1). We are doing all...
2
by: Alex Popescu | last post by:
Hi all! not be present in Py3k. So, I am wondering what will be its replacement? Considering that most probably Py3k will keep eval and exec, this will still be possible (indeed requiring manual...
1
by: Fernando Perez | last post by:
Hi all, I'm finding the following behavior truly puzzling, but before I post a bug report on the site, I'd rather be corrected if I'm just missing somethin obvious. Consider the following...
5
by: George Sakkis | last post by:
I maintain a few configuration files in Python syntax (mainly nested dicts of ints and strings) and use execfile() to read them back to Python. This has been working great; it combines the...
1
by: moijes12 | last post by:
Hi i have 3 python files and i want to execute the files sequentially using the execfile command.Hence ,i have written the following program fileList = for fileName in fileList :...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.