<div dir="ltr">Hi,<br><br>Recently I tried to execute a python file using execfile (exec performed just the same for that reason).<br>I encountered the behavior below: <br><br>"""<br>$ cat execme.py<br>a = 2<br>
$ python<br>Python 2.4.3 (#1, May 24 2008, 13:57:05) <br>[GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2<br>Type "help", "copyright", "credits" or "license" for more information.<br>
>>> def execfile_func():<br>...     execfile('execme.py')<br>...     print 'locals() = %s' % str(locals())<br>...     print a<br>... <br>>>> execfile_func()<br>locals() = {'a': 2}<br>
Traceback (most recent call last):<br>  File "<stdin>", line 1, in ?<br>  File "<stdin>", line 4, in execfile_func<br>NameError: global name 'a' is not defined<br>>>><br>"""<br>
<br>After execfile, the a variable can be found in locals(), however any direct reference (e.g., print a) fails.<br>Is it expected?<br><br>Thanks,<br>Doron<br></div>