nested_scopes with execfile
Amir Hadar
amir.hadar at israel.sun.com
Tue Mar 19 11:45:59 EST 2002
Hi
I've encountered a problem when executing a file within a function:
#----- Test1.py ------------------
from __future__ import nested_scopes
def f():
execfile("Test2.py")
f()
# EOF Test1.py -------------------
#--------Test2.py ----------------
def f1():
print "OK it works"
def f2():
f1()
f2()
#EOF Test2.py --------------------
When executing the Test1.py the following error is displayed:
Traceback (most recent call last):
File "<string>", line 1, in ?
File "C:\PROGRA~1\Python22\Test1.py", line 6, in ?
f()
File "C:\PROGRA~1\Python22\Test1.py", line 4, in f
execfile("Test2.py");
File "Test2.py", line 8, in ?
f2()
File "Test2.py", line 6, in f2
f1()
NameError: global name 'f1' is not defined
The reson that I need this is because I want to make a "Load" funtion
that will execute the given file (e.g. Load("Yada.py")) and then will
run a function in that file (e.g. def Init():).
In the example I gave the second file ("Test2.py") is loaded to the
local environment of the function f() in the first file (Test1.py).
Therefore the functions f1 and f2 are in that env. Now, When f2 calls
f1 it should resolve its variables in the enclosing environment which
is the env of f (Test1.py) but it doen't.
Help Me.
Amir.
More information about the Python-list
mailing list