aosabook/500lines · error · NameError

global name '%s' is not defined

Error message

global name '%s' is not defined

What it means

Error "global name '%s' is not defined" thrown in aosabook/500lines.

Source

Thrown at interpreter/code/byterun/pyvm2.py:334

        if name in self.frame.local_names:
            val = self.frame.local_names[name]
        else:
            raise UnboundLocalError(
                "local variable '%s' referenced before assignment" % name
            )
        self.frame.push(val)

    def byte_STORE_FAST(self, name):
        self.frame.local_names[name] = self.frame.pop()

    def byte_LOAD_GLOBAL(self, name):
        f = self.frame
        if name in f.global_names:
            val = f.global_names[name]
        elif name in f.builtin_names:
            val = f.builtin_names[name]
        else:
            raise NameError("global name '%s' is not defined" % name)
        f.push(val)

    ## Operators

    UNARY_OPERATORS = {
        'POSITIVE': operator.pos,
        'NEGATIVE': operator.neg,
        'NOT':      operator.not_,
        'INVERT':   operator.invert,
    }

    def unaryOperator(self, op):
        x = self.frame.pop()
        self.frame.push(self.UNARY_OPERATORS[op](x))

    BINARY_OPERATORS = {
        'POWER':    pow,
        'MULTIPLY': operator.mul,

View on GitHub (pinned to fba689d101)

Solutions

  1. Define the global at module level before the function that reads it runs.
  2. Check the spelling of the global name and that the module defining it was imported.
  3. If the name should be a builtin, verify it exists in this Python version or provide it explicitly.

Example fix

counter = 0
def bump():
    global counter
    counter += 1  # global defined at module scope before bump() is called

When it happens

Trigger: Thrown at interpreter/code/byterun/pyvm2.py:334 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of aosabook/500lines@fba689d101 (2026-08-13). Data as JSON: /api/errors/457312b68a4402b3. Report an issue: GitHub.