Isolated environment for execfile

Discussion in 'Python' started by Igor Kaplan, Oct 1, 2008.

  1. Igor Kaplan

    Igor Kaplan Guest

    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.
    Igor Kaplan, Oct 1, 2008
    #1
    1. Advertising

  2. Igor Kaplan

    Lie Ryan Guest

    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.
    Lie Ryan, Oct 1, 2008
    #2
    1. Advertising

  3. En Wed, 01 Oct 2008 08:11:29 -0300, Igor Kaplan
    <> 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
    Gabriel Genellina, Oct 2, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Namratha Shah \(Nasha\)
    Replies:
    0
    Views:
    9,005
    Namratha Shah \(Nasha\)
    Nov 17, 2004
  2. =?Utf-8?B?Sm9obiBUYWNrZQ==?=

    Isolated Development model problems

    =?Utf-8?B?Sm9obiBUYWNrZQ==?=, Mar 1, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    410
    John Saunders
    Mar 1, 2004
  3. =?Utf-8?B?R2Fs?=
    Replies:
    0
    Views:
    388
    =?Utf-8?B?R2Fs?=
    Jun 17, 2004
  4. =?Utf-8?B?UmFscGg=?=

    Setting up an Isolated .NET Team Environment

    =?Utf-8?B?UmFscGg=?=, Mar 28, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    410
    =?Utf-8?B?UmFscGg=?=
    Apr 8, 2005
  5. Wm. Scott Miller

    Isolated Development on IIS 5.1 & 6.0

    Wm. Scott Miller, Mar 17, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    2,813
    Erik Funkenbusch
    Mar 18, 2006
Loading...

Share This Page