Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Curious 'name not defined error' with 'python -m' #54338

Closed
inducer mannequin opened this issue Oct 17, 2010 · 2 comments
Closed

Curious 'name not defined error' with 'python -m' #54338

inducer mannequin opened this issue Oct 17, 2010 · 2 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@inducer
Copy link
Mannequin

inducer mannequin commented Oct 17, 2010

BPO 10129
Nosy @birkenfeld

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2010-10-17.06:03:09.629>
created_at = <Date 2010-10-17.03:42:25.813>
labels = ['interpreter-core', 'type-bug']
title = "Curious 'name not defined error' with 'python -m'"
updated_at = <Date 2010-10-17.06:03:09.611>
user = 'https://bugs.python.org/inducer'

bugs.python.org fields:

activity = <Date 2010-10-17.06:03:09.611>
actor = 'georg.brandl'
assignee = 'none'
closed = True
closed_date = <Date 2010-10-17.06:03:09.629>
closer = 'georg.brandl'
components = ['Interpreter Core']
creation = <Date 2010-10-17.03:42:25.813>
creator = 'inducer'
dependencies = []
files = []
hgrepos = []
issue_num = 10129
keywords = []
message_count = 2.0
messages = ['118913', '118915']
nosy_count = 2.0
nosy_names = ['georg.brandl', 'inducer']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue10129'
versions = ['Python 2.6', 'Python 3.1']

@inducer
Copy link
Mannequin Author

inducer mannequin commented Oct 17, 2010

$ 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()

@inducer inducer mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Oct 17, 2010
@birkenfeld
Copy link
Member

(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)

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant