How To Fix Recursionerror In Python

Last Updated : 1 Jul, 2026

A RecursionError: maximum recursion depth exceeded, occurs when a recursive function makes too many recursive calls without stopping. This usually happens because the function has no proper base case, contains incorrect recursive logic, or exceeds Python's recursion limit. When this limit is reached, Python raises a RecursionError to prevent the program from crashing.

Python
def endless_recursion():
    return endless_recursion()

endless_recursion()

Output

ERROR!
Traceback (most recent call last):
File "<main.py>", line 4, in <module>
File "<main.py>", line 2, in endless_recursion
File "<main.py>", line 2, in endless_recursion
File "<main.py>", line 2, in endless_recursion
[Previous line repeated 995 more times]
RecursionError: maximum recursion depth exceeded

Explanation: function continuously calls itself without any stopping condition. Since Python cannot terminate the recursive calls, it eventually reaches the maximum recursion depth and raises a RecursionError.

Common Causes

1. Missing Base Case: A recursive function must contain a condition that stops further recursive calls. Without a base case, the function keeps calling itself indefinitely.

Python
def factorial(n):
    return n * factorial(n - 1)

print(factorial(5))

Output

ERROR!
Traceback (most recent call last):
File "<main.py>", line 4, in <module>
File "<main.py>", line 2, in factorial
File "<main.py>", line 2, in factorial
File "<main.py>", line 2, in factorial
[Previous line repeated 995 more times]
RecursionError: maximum recursion depth exceeded

Explanation: function keeps calling itself with smaller values of n, but there is no condition to stop when n reaches 0. As a result, recursion continues forever until Python reaches its recursion limit.

2. Infinite Recursion Due to Incorrect Logic: Even when a base case exists, incorrect recursive logic can prevent the function from ever reaching it.

Python
def countdown(n):
    if n > 0:
        print(n)

        # n is never reduced
        countdown(n)

countdown(5)

Output

5
5
5
.
.
.
ERROR!
Traceback (most recent call last):
File "<main.py>", line 8, in <module>
File "<main.py>", line 6, in countdown
File "<main.py>", line 6, in countdown
File "<main.py>", line 6, in countdown
[Previous line repeated 995 more times]
RecursionError: maximum recursion depth exceeded

Explanation: Although the function checks whether n > 0, the value of n never changes. Therefore, the condition always remains true and the function keeps calling itself forever.

3. Exceeding Python's Recursion Limit: Sometimes the recursive logic is correct, but the recursion depth is so large that it exceeds Python's default recursion limit.

Python
def factorial(n):
    if n == 0:
        return 1

    return n * factorial(n - 1)
print(factorial(1001))

Output

ERROR!
Traceback (most recent call last):
File "<main.py>", line 6, in <module>
File "<main.py>", line 5, in factorial
File "<main.py>", line 5, in factorial
File "<main.py>", line 5, in factorial
[Previous line repeated 995 more times]
RecursionError: maximum recursion depth exceeded

Explanation: function is implemented correctly and contains a base case. However, calculating the factorial of 1001 requires more recursive calls than Python's default recursion limit allows.

Fixing RecursionError

1. Add a Proper Base Case: most important requirement of a recursive function is a valid base case that eventually stops recursion..

Python
def factorial(n):
    if n == 0:
        return 1

    return n * factorial(n - 1)
print(factorial(5))

Output
120

Explanation: When n becomes 0, the function returns 1 instead of making another recursive call. This stops the recursion and allows the result to be calculated correctly.

2. Increase the Recursion Limit: If deep recursion is unavoidable, Python allows the recursion limit to be increased using the sys module.

Python
import sys
sys.setrecursionlimit(10000)

def factorial(n):
    if n == 0:
        return 1

    return n * factorial(n - 1)
print(factorial(1001))

Output
40278964733717086731724613635692698970509423907492534717634371034036845091102764961263625269545637420528046859880739325469029853986780336746022515349961453558842192859116083367874245135491592125229928...

Explanation: sys.setrecursionlimit() increases the maximum number of recursive calls Python allows. This can help when working with deeply recursive algorithms, but it should be used carefully because very large recursion depths may consume significant memory.

3. Use an Iterative Approach: In many cases, recursion can be replaced with loops, which avoid recursion limits entirely.

Python
def factorial_iterative(n):
    result = 1

    for i in range(1, n + 1):
        result *= i

    return result
print(factorial_iterative(1001))

Output
40278964733717086731724613635692698970509423907492534717634371034036845091102764961263625269545637420528046859880739325469029853986780336746022515349961453558842192859116083367874245135491592125229928...

Explanation: Instead of repeatedly calling the same function, a loop performs the calculation step by step. Since no recursive calls are involved, there is no risk of exceeding Python's recursion limit.

Comment