[New-bugs-announce] [issue16781] execfile/exec messes up with imports in executed file

anatoly techtonik report at bugs.python.org
Wed Dec 26 01:15:50 CET 2012

New submission from anatoly techtonik:

When a Python file is exec()uted, it magically fails to find names in imported modules. The most magical thing in the examples below (b3.py in attach for Python 3) is that first reference to wintypes.LONG in print statement is actually successfull.

--- a.py
from ctypes import wintypes


class LOGFONT(object):
  field = wintypes.LONG

--- b2.py (Python 2 version)
def main():

--- Output
<class 'ctypes.c_long'>
Traceback (most recent call last):
  File "b2.py", line 4, in <module>
  File "b2.py", line 2, in main
  File "a.py", line 5, in <module>
    class LOGFONT(object):
  File "a.py", line 6, in LOGFONT
    field = wintypes.LONG
NameError: name 'wintypes' is not defined

components: Interpreter Core
files: b3.py
messages: 178177
nosy: techtonik
priority: normal
severity: normal
status: open
title: execfile/exec messes up with imports in executed file
versions: Python 2.6, Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4
Added file: http://bugs.python.org/file28434/b3.py

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list