python runtime_error official true

RecursionError

ID: python/recursionerror

Also available as: JSON · Markdown
80%Fix Rate
95%Confidence
0Evidence

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

The maximum recursion depth was exceeded. A recursive function has too many nested calls, usually due to a missing or broken base case.

generic

Official Documentation

https://docs.python.org/3/library/exceptions.html

Workarounds

  1. 90% success Check that the recursive function has a proper base case that terminates
    Add a condition that stops recursion (e.g., if n <= 1: return 1)
  2. 90% success Convert the recursive algorithm to an iterative one using a loop + explicit stack
    Use while loop with a list as a manual stack

Dead Ends

Common approaches that don't work:

  1. Increasing sys.setrecursionlimit() without fixing the algorithm 80% fail

    May cause C stack overflow and process crash; does not fix infinite recursion