On this page
article
RecursionError: maximum recursion depth (variant 9)
How to fix RecursionError in Python — maximum recursion depth.
Error Message
RecursionError: maximum recursion depth
What It Means
Python raised RecursionError because something in your code violated language rules or runtime expectations.
Common Causes
- Typo in variable, function, or module name
- Wrong data type for the operation
- Missing import or uninstalled dependency
- Incorrect indentation or syntax
How to Fix
Add base case; consider iterative solution.
Example
# Problem scenario related to RecursionError
# ... code that triggers the error ...
# Fixed version
# Apply the fix described above
Prevention
- Run
python -m py_compile script.pybefore committing - Use a linter (
ruff,flake8) and type checker (mypy) - Write tests for edge cases (empty input, None, zero)