{"record":{"id":"436059c7463cba5c","repo":"aosabook/500lines","slug":"name-s-is-not-defined-436059","errorCode":null,"errorMessage":"name '%s' is not defined","messagePattern":"name '(.+?)' is not defined","errorType":"exception","errorClass":"NameError","httpStatus":null,"severity":"error","filePath":"interpreter/code/byterun/simple_python_interpreter.py","lineNumber":232,"sourceCode":"        self.push(const)\n\n    def byte_POP_TOP(self):\n        self.pop()\n\n    def byte_DUP_TOP(self):\n        self.push(self.top())\n\n    ## Names\n    def byte_LOAD_NAME(self, name):\n        frame = self.frame\n        if name in frame.f_locals:\n            val = frame.f_locals[name]\n        elif name in frame.f_globals:\n            val = frame.f_globals[name]\n        elif name in frame.f_builtins:\n            val = frame.f_builtins[name]\n        else:\n            raise NameError(\"name '%s' is not defined\" % name)\n        self.push(val)\n\n    def byte_STORE_NAME(self, name):\n        self.frame.f_locals[name] = self.pop()\n\n    def byte_DELETE_NAME(self, name):\n        del self.frame.f_locals[name]\n\n    def byte_LOAD_FAST(self, name):\n        if name in self.frame.f_locals:\n            val = self.frame.f_locals[name]\n        else:\n            raise UnboundLocalError(\n                \"local variable '%s' referenced before assignment\" % name\n            )\n        self.push(val)\n\n    def byte_STORE_FAST(self, name):","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/aosabook/500lines/blob/fba689d101eb5600f5c8f4d7fd79912498e950e2/interpreter/code/byterun/simple_python_interpreter.py#L214-L250","documentation":"Raised by byte_LOAD_NAME after the interpreter searched frame.f_locals, frame.f_globals, and frame.f_builtins without finding the name. This byterun-style VM mirrors CPython's NameError for lookups that start in the local scope (module bodies, class bodies, exec'd code). LOAD_NAME is the opcode emitted for those scopes, so any unbound name there reaches this raise.","triggerScenarios":"Executing bytecode whose LOAD_NAME target was never bound: a top-level name referenced at module scope that has no entry in f_locals/f_globals and is not a builtin; a name that was just removed with DELETE_NAME; running code compiled for a different module than the one whose globals dict is loaded into the frame.","commonSituations":"Typo in a module-level variable or function name; a top-level import that was placed inside a conditional branch that never ran; referencing a name before its module-level assignment; a failed/missing import so the name never lands in f_globals.","solutions":["Confirm the name is defined or imported at module scope before the bytecode runs (inspect frame.f_globals).","If the name should be a builtin, verify frame.f_builtins is populated with the builtins module.","Use dis.dis() on the code object to find the offending LOAD_NAME and compare it to the namespaces you expect to be in scope.","Wrap the interpreter run in a try/except NameError to log the offending name and continue."],"exampleFix":"// before\nprint(undefined_name)   # LOAD_NAME -> NameError\n// after\nundefined_name = 42\nprint(undefined_name)","handlingStrategy":"try-catch","validationCode":"def name_resolvable(name, frame):\n    return name in frame.f_locals or name in frame.f_globals or name in frame.f_builtins\n\n# before running the code object\nfor instr in dis.get_instructions(code):\n    if instr.opname == 'LOAD_NAME' and not name_resolvable(instr.argval, frame):\n        print('missing name:', instr.argval)","typeGuard":null,"tryCatchPattern":"try:\n    interpreter.run_frame(frame)\nexcept NameError as e:\n    log.warning('undefined name during exec: %s', e)","preventionTips":["Lint source with pyflakes/pylint to catch undefined names before execution.","Populate frame.f_builtins from the builtins module so builtins always resolve.","Keep imports at module top-level so names land in f_globals deterministically."],"tags":["name-resolution","interpreter","python","runtime","bytecode"],"backgroundTag":null,"analyzedSha":"fba689d101eb5600f5c8f4d7fd79912498e950e2","analyzedAt":"2026-08-13T06:26:32.792Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}