Maximum recursion depth reached in Python

In this post, we will see about Maximum recursion depth reached in Python.This can be solved by increase the recursion depth in python.

If you are getting Runtime error Maximum recursion depth reached. then you can increase default recursion depth which is by default 1000.
You need to import sys module in your script and put following code.

It will increase default recursion depth to 1500.

But I won’t recommend using this tactic instead you should rewrite your algorithm to an iterative one if possible. Python does not optimize tail recursion and uncontrolled recusion causes stack overflows.

For example:
Instead of fibonacci recusive algoroithm

You can use iterative algorithm.

That’s all about Maximum recursion depth reached in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *