Python RecursionError: maximum recursion depth exceeded
A plain-words explanation of this Python message: what it means, the usual causes, and the steps that fix it.
The messageRecursionError: maximum recursion depth exceeded
Make the error yourself
Run with Python 3.14.10. This is the smallest example we could find that shows it.
Python program (main.py)
def countdown(n):
print(n)
countdown(n - 1)
countdown(3)Output (the real message)
Traceback (most recent call last):
File "main.py", line 5, in <module>
countdown(3)
~~~~~~~~~^^^
File "main.py", line 3, in countdown
countdown(n - 1)
~~~~~~~~~^^^^^^^
File "main.py", line 3, in countdown
countdown(n - 1)
~~~~~~~~~^^^^^^^
File "main.py", line 3, in countdown
countdown(n - 1)
~~~~~~~~~^^^^^^^
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceededWhy it happens
A function that calls itself is recursive. Each call waits on the call it made, and Python stores every waiting call in memory. Python refuses to go deeper than a limit (about a thousand calls by default) to protect the computer, and raises this error. The usual reason is a missing or unreachable base case: the condition that stops the calls. In the broken example the countdown never stops, because nothing tests when n has gone far enough.
How to fix it
- Add a base case at the start:
if n < 0: return. - Make sure every call moves towards that case (here
n - 1). - For very deep but valid work, rewrite it as a loop instead of recursion.
- Do not raise the limit with
sys.setrecursionlimitto hide the problem; a missing base case will just run for longer and then crash harder.
The corrected version
Python program (main.py)
def countdown(n):
if n < 0:
return
print(n)
countdown(n - 1)
countdown(3)Output (the corrected version)
3
2
1
0How to avoid it
Write the base case first, then the recursive step, and try it with the smallest input.
Questions
What is the default recursion limit?
You can read it with import sys; print(sys.getrecursionlimit()). It is about 1000 in standard Python.
This is an independent guide. It is not connected with Python, Microsoft, Google, GitHub or the Git project. Product names belong to their owners. Examples were run on 13 November 2026 in a practice folder or a blank workbook; your version may word a message slightly differently.