{"record":{"id":"b2078d3858cb683b","repo":"aosabook/500lines","slug":"global-name-s-is-not-defined-b2078d","errorCode":null,"errorMessage":"global name '%s' is not defined","messagePattern":"global name '(.+?)' is not defined","errorType":"exception","errorClass":"NameError","httpStatus":null,"severity":"error","filePath":"interpreter/code/byterun/simple_python_interpreter.py","lineNumber":260,"sourceCode":"        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):\n        self.frame.f_locals[name] = self.pop()\n\n    def byte_LOAD_GLOBAL(self, name):\n        f = self.frame\n        if name in f.f_globals:\n            val = f.f_globals[name]\n        elif name in f.f_builtins:\n            val = f.f_builtins[name]\n        else:\n            raise NameError(\"global name '%s' is not defined\" % name)\n        self.push(val)\n\n    ## Operators\n\n    UNARY_OPERATORS = {\n        'POSITIVE': operator.pos,\n        'NEGATIVE': operator.neg,\n        'NOT':      operator.not_,\n        'INVERT':   operator.invert,\n    }\n\n    def unaryOperator(self, op):\n        x = self.pop()\n        self.push(self.UNARY_OPERATORS[op](x))\n\n    BINARY_OPERATORS = {\n        'POWER':    pow,\n        'MULTIPLY': operator.mul,","sourceCodeStart":242,"sourceCodeEnd":278,"githubUrl":"https://github.com/aosabook/500lines/blob/fba689d101eb5600f5c8f4d7fd79912498e950e2/interpreter/code/byterun/simple_python_interpreter.py#L242-L278","documentation":"Raised by byte_LOAD_GLOBAL when a name the compiler marked as global is absent from both frame.f_globals and frame.f_builtins. Unlike LOAD_NAME, the fast-local dict is skipped entirely; global lookups check only the module's globals and the builtins namespace. This matches CPython's NameError for names the compiler knows are module/builtin level (anything not assigned inside the function).","triggerScenarios":"A function references a name treated as global (never assigned in the function, hence LOAD_GLOBAL) that was never imported or defined at module scope and is not a builtin, e.g. calling a helper defined in another module that was never imported into the running module's globals.","commonSituations":"Forgetting to import a module-level function/class used inside a function; a `del` that removed the global; a typo in a builtin name (e.g. `lenght`); running the function against a different module's globals dict than expected.","solutions":["Import or define the missing name at module scope so it lands in frame.f_globals.","If the name is a builtin, correct the spelling so it resolves through frame.f_builtins.","Pass the dependency in as an explicit argument instead of relying on the global lookup."],"exampleFix":"// before\ndef f():\n    return helper()   # LOAD_GLOBAL -> NameError (helper never imported)\n// after\nfrom utils import helper\ndef f():\n    return helper()","handlingStrategy":"try-catch","validationCode":"def global_resolvable(name, frame):\n    return name in frame.f_globals or name in frame.f_builtins\n\nfor instr in dis.get_instructions(code):\n    if instr.opname == 'LOAD_GLOBAL' and not global_resolvable(instr.argval, frame):\n        print('missing global:', instr.argval)","typeGuard":null,"tryCatchPattern":"try:\n    interpreter.run_frame(frame)\nexcept NameError as e:\n    log.warning('undefined global during exec: %s', e)","preventionTips":["Import all module-level names a function relies on at module top level.","Pass cross-module dependencies as explicit arguments.","Lint to flag undefined globals before running bytecode."],"tags":["name-resolution","interpreter","python","global-scope","bytecode"],"backgroundTag":null,"analyzedSha":"fba689d101eb5600f5c8f4d7fd79912498e950e2","analyzedAt":"2026-08-13T06:26:32.792Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}