{
  "id": 19172640,
  "title": "UnboundLocalError: cannot access local variable where it is not associated with a value in Python: 10 Causes and Fixes (Tested Examples)",
  "url": "https://urgent.news/2026/11/09/unboundlocalerror-cannot-access-local-variable-where-it-is-not",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-09T15:49:15.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/mfaysaldev/unboundlocalerror-cannot-access-local-variable-where-it-is-not-associated-with-a-value-in-python-b5l"
  },
  "original_language": "en",
  "account": "UnboundLocalError: cannot access local variable x where it is not associated with a value is an error in Python that occurs when a function tries to read a local variable, but Python has determined that variable has not yet been given a value. This error can happen for several reasons, often due to the way variables are assigned and accessed within functions. Here are 10 common causes of this error, along with fixes for each case:\n\n1. Augmented assignment (+=) to a global variable: When you use augmented assignment operators like += on a variable that is defined as global inside a function, Python treats the variable as local. To fix this, declare the variable as global using the global keyword before using it.\n\n2. Reading a global, then assigning it later: If you read a global variable and then assign a value to it later within the function, Python will raise an UnboundLocalError. To resolve this, make sure to declare the variable as global before assigning a value to it.\n\n3. Variable assigned only inside an if branch: If a variable is assigned a value only inside an if statement and that if statement is never executed, Python will treat the variable as local and raise an UnboundLocalError when you try to access it outside the if block. To avoid this, set a default value for the variable before the if statement.\n\n4. Using a loop variable after an empty loop: When you use a loop variable after a loop that doesn't run any iterations (e.g., an empty loop), Python will consider that variable as local and raise an UnboundLocalError if you try to access it. To fix this, initialize the variable before the loop or handle the case where the loop doesn't run.\n\n5. A try block that failed before the assignment: If an exception occurs in a try block before the assignment of a local variable, Python will raise an UnboundLocalError when you try to access that variable in the except or finally block. To handle this, ensure that the variable is assigned a value before the try block or use the walrus operator (:=) to assign the value within the try block.\n\n6. Nested function updating an outer variable: When a nested function modifies an outer variable using augmented assignment (e.g., +=), Python will treat the outer variable as local and raise an UnboundLocalError. To avoid this, declare the outer variable as nonlocal within the nested function.\n\n7. Reading a name after del: If you use the del statement to delete a variable and then try to read it afterward, Python will raise an UnboundLocalError. To prevent this, do not reuse a deleted variable name.\n\n8. Import later in the same function: If you import a module later in a function, any names defined in that module will be considered local to the function. To avoid this error, move the import statements to the top of the function.\n\n9. Using an except ... as variable after the block: If you use an except block with the as keyword to assign a variable (e.g., except Exception as err) and then try to access that variable outside the except block, Python will raise an UnboundLocalError. To fix this, copy the value to another variable before assigning it to the except block.\n\n10. Shadowing a built-in name like len: If you accidentally assign a value to a built-in name like len inside a function, Python will treat that name as local and raise an UnboundLocalError when you try to use the built-in function later. To avoid this, pick a different variable name when assigning a value.\n\nTo quickly identify the cause of an UnboundLocalError, you can use the following steps:\n\n1. Check if the variable is assigned a value before it is read.\n2. Verify if the variable is assigned a value within an if branch, loop, try block, or nested function.\n3. Ensure that you are not shadowing a built-in name.\n4. Check if the variable is declared as global or nonlocal when needed.\n\nBy following these guidelines and fixing the specific cause of the UnboundLocalError, you can resolve this common Python error in your code.",
  "summary": "Python raises UnboundLocalError: cannot access local variable 'x' where it is not associated with a value when a function reads a name that Python has decided is local to that function, but the line that gives it a value has not run yet. Any assignment to a name anywhere inside a function (including += , for , import , del and except ... as ) makes that name local for the whole function. The…",
  "key_points": [],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}