[docs] [issue14049] execfile() fails on files that use global variables inside functions

anatoly techtonik report at bugs.python.org
Sat Feb 18 13:59:58 CET 2012


New submission from anatoly techtonik <techtonik at gmail.com>:

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

----------
assignee: docs at python
components: Documentation, Library (Lib)
messages: 153643
nosy: docs at python, techtonik
priority: normal
severity: normal
status: open
title: execfile() fails on files that use global variables inside functions
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14049>
_______________________________________


More information about the docs mailing list