main.py below fails to execute local.py, which work ok (outputs '2') when processed directly in console.
Docs are not explaining anything. They spread fear and uncertainty around locals, but nothing is said why globals may fail.
http://docs.python.org/library/functions.html#execfile
---[cut main.py]---
values = {}
glavues = {}
execfile('local.py', glavues, values)
---[/cut]-----------
---[cut local.py]---
x = 1
def weird():
y = x + 1
return y
print weird()
---[/cut]-----------
Traceback (most recent call last):
File "main.py", line 5, in <module>
execfile('local.py', glavues, values)
File "local.py", line 7, in <module>
print weird()
File "local.py", line 5, in weird
y = x + 1
NameError: global name 'x' is not defined |