This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Curious 'name not defined error' with 'python -m'
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.1, Python 2.6
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, inducer
Priority: normal Keywords:

Created on 2010-10-17 03:42 by inducer, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg118913 - (view) Author: Andreas Kloeckner (inducer) Date: 2010-10-17 03:42
$ python3.1 -m a b.py

results in

Traceback (most recent call last):
  File "/usr/lib/python3.1/runpy.py", line 128, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python3.1/runpy.py", line 34, in _run_code
    exec(code, run_globals)
  File "/home/andreas/tmp/python-mbug/a.py", line 6, in <module>
    main()
  File "/home/andreas/tmp/python-mbug/a.py", line 3, in main
    exec(compile(open(sys.argv[1]).read(), sys.argv[1], 'exec'))
  File "b.py", line 8, in <module>
    sys.exit(main())
  File "b.py", line 5, in main
    argv = sys.argv[1:]
NameError: global name 'sys' is not defined

a.py --------------------------------------------------
def main():
    import sys
    exec(compile(open(sys.argv[1]).read(), sys.argv[1], 'exec'))

if __name__=='__main__':
    main()
-------------------------------------------------------

b.py --------------------------------------------------
import sys

def main():
  sys.argv[1:]

if __name__ == "__main__":
  main()
-------------------------------------------------------
msg118915 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-10-17 06:03
(This is not specific to running with -m, it occurs as well when you do "python a.py b.py".)

The issue here is your call to exec() does not execute the code as its own module.  It executes the code as part of the main() function in a.py, with (since you don't give a global namespace argument) the same global namespace as that of a.main(), and there is no "sys" in that namespace.  Further, the compiler cannot treat b.main like a nested function of a.main (which would then create a closure over the "sys" imported inside a.main).  Therefore, the reference to sys is treated as a global and not found.

If you give the exec() function an explicit dictionary as the globals argument, this "works":

def main():
    d = {'__name__': '__main__'}
    exec(compile(open(sys.argv[1]).read(), sys.argv[1], 'exec'), d)
History
Date User Action Args
2022-04-11 14:57:07adminsetgithub: 54338
2010-10-17 06:03:09georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg118915

resolution: wont fix
2010-10-17 03:42:25inducercreate