RecursionError in Python — What It Means and How to Fix It

What Python's RecursionError exception means and how to resolve it.

Published October 28, 2026

RecursionError is raised when a function calls itself too deeply, exceeding Python's recursion limit (1000 by default).

What it means and why it happens

Usually the base case is missing or never reached, or the problem is too deep for recursion.

Read the full message: it names the file and line where the problem was detected, which is the best place to start.

What you see

run the code
RecursionError: maximum recursion depth exceeded

Practical examples

Code that triggers it

def f(n):
    return f(n - 1)   # no base case
f(5)

The fix

def f(n):
    if n <= 0:
        return 0
    return n + f(n - 1)

Iterate instead

def total(n):
    s = 0
    for i in range(1, n + 1): s += i
    return s

Common mistakes

  • Missing base case.
  • Raising sys.setrecursionlimit instead of fixing logic.
  • Deep recursion on large inputs.

Key takeaways

  • RecursionError: RecursionError is raised when a function calls itself too deeply, exceeding Python's recursion limit (1000 by default).
  • Read the message and the line number first.
  • Fix the cause, don't just silence the error.

FAQ

Can I raise the limit?

sys.setrecursionlimit, but iteration is usually better.

How do I find which line caused RecursionError?

The error message and stack trace show the file and line; start there and work backwards.

More Python articles