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('scrip t2.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('scrip t2.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. 2 1629
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('scrip t2.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.
En Wed, 01 Oct 2008 08:11:29 -0300, Igor Kaplan
<ig************ *************** ******@hotmail. comescribió:
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('scrip t2.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
print "x:", x
f()
|
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 exception ?
Any suggestions
Bo.
|
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 app uses the .NET Isolated storage libraries for
storing connection string and other info about the application. The
problem is that the Isolated storage bombs at design time (when you
try and view a control that hits the database at design time).
|
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 have a few problems that I am trying to work around.
1) All our sites are created using virtual paths. If I am to adopt the
isolated model and develop on each developers machine, when you create
|
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 ASP.NET applications.
We have been using a non-isolated development model (all development happens
directly on a server) and it has worked fine with only two developers, but
we are expanding and it is no longer an option. The model that we are...
| |
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 loading of the file string), so
I would really appreciate some enlightning comments on this.
Background:
Basically this question is related to my learning process/working project.
In this, I have the need to allow the final user to provide a...
|
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 trivial script:
# Simple script that imports something from the stdlib
from math import sin, pi
|
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 convenience of
pickle with the readability of Python. So far each configuration is
contained in a single standalone file; different configurations are
completely separate files.
Now I'd like to factor out the commonalities of the different...
|
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 :
execfile(fileName)
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |