Recursionerror maximum recursion depth exceeded while calling a python object pyinstaller

How To Fix RecursionError: maximum recursion depth exceeded in Python

Recursive functions are functions that call themselves during execution, the RecursionError: maximum recursion depth exceeded occurs because the recursive function you use has exceeded Python’s limits. This guide will show the cause of the error so you can fix it in your program.

Why does the error RecursionError: maximum recursion depth exceeded occur

A safe recursive function has bounds added to it to ensure they don’t execute infinitely. It means the function will stop when it finds an answer or a specific condition is met. An error will occur if you write a recursive function that crosses the limit in Python. Let’s see an example of the error in the example below.

Error Example:

An example of a recursive function counting numbers beyond the allowed limit of Python

def CountNumber(count): count = count + 1 # Can't make recursion through 1000 times because the limit is exceeded if count 
RecursionError: maximum recursion depth exceeded

The error occurs because the number of executions of the recursive function has exceeded the 1000 times limit in Python, which is a Python mechanism to help the program avoid memory overflow. To solve this error, we will use the following solutions.

Solution For The RecursionError: maximum recursion depth exceeded

If Python does not have this protection, our program will execute until no memory is left. We can work around this error by using an alternate loop solution or increasing the recursion limit.

Use loop

We can change the function recursively by the loop to avoid the error.

def CountNumber(count): # Use a For loop to replace a recursive function for index in range(count, 1000): print(index) CountNumber(0)

The error does not occur because we use a loop instead of a recursive function. But if you must use the recursive function, you can set the recursion number.

Using setrecursionlimit()

You can bypass Python’s default recursion limit by using the setrecursionlimit() method to set a new limit.

import sys # Using the setrecursionlimit() method to set a new recursion limit. sys.setrecursionlimit(2000) def CountNumber(count): count = count + 1 if count 

The error was fixed because we increased the recursion limit in Python to 2000

Summary

Through the article, we have understood the cause of the RecursionError: maximum recursion depth exceeded error in Python and the solutions to fix it. We often use an alternative loop because this is the best way. Hope you get the problem resolved soon.

Carolyn Hise has three years of software development expertise. Strong familiarity with the following languages is required: Python, Typescript/Nodejs, .Net, Java, C++, and a strong foundation in Object-oriented programming (OOP).

Источник

How to fix the RecursionError: maximum recursion depth exceeded in Python

When running a Python program, you might encounter the following error:

After doing some research, I found that this error occurs when a recursive function exceeded the default recursion limit set by Python, which is 1000 times.

Usually, this error occurs because of 2 possible scenarios:

  • You don’t have a base case to stop the recursion
  • Your recursive code requires more than 1000 depth

This tutorial will help you solve the error in both scenarios.

You don’t have a base case to stop the recursion

In practice, a recursive function will always call itself until a specific condition is met that stops the recursion.

That specific condition is also known as the base case, and your code must be able to reach that condition to stop running.

For example, the following countup() function prints the number n recursively:

But since there’s no base case in this function, it will call itself forever, eventually causing a stack overflow.

To prevent the code from running forever, Python placed a soft limit on recursion to 1000 depth. This means when the function calls itself up to 1000 times, Python stops the execution and raises an error:

To resolve this error, you need to add a base case that prevents the recursion from running infinitely.

For example, stop the countup() function when it reaches 10 as shown below:


This code won’t cause a RecursionError because as soon as the n variable is 10 or greater, the recursion won’t happen.

Your recursive code requires more than 1000 depth

Python sets the limit of recursion to 1000 to avoid recursive code repeating forever.

You can find this out by calling the getrecursionlimit() function from the sys module:

To get over this limitation, you can set the recursion limit using the setrecursionlimit() function.

This code modifies the recursion limit to 1500:

But keep in mind that increasing the limit too many would degrade the performance of Python itself.

If you could, you should use the iterative approach using a while loop instead of recursion. For example, here’s a modified countup() function that uses the iterative approach:

You can use the base case as the condition that tells the while loop when to stop, and do the required operations inside the while block. At the end of the block, add an operation that allows the while loop to reach the base case.

Conclusion

Python raises the RecursionError: maximum recursion depth exceeded while calling a Python object when your recursive code exceeded the maximum recursion depth, which is set to 1000 by default.

To resolve this error, you need to create a base case to stop the recursion from going forever, and make sure that the base case can be reached within the allowed recursion depth.

You can change the recursion depth limit, or you can also use an iterative approach.

I hope this tutorial helps. See you in other tutorials! 👋

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

About

Hello! This website is dedicated to help you learn tech and data science skills with its step-by-step, beginner-friendly tutorials.
Learn statistics, JavaScript and other programming languages using clear examples written for people.

Type the keyword below and hit enter

Tags

Click to see all tutorials tagged with:

Источник

Python RecursionError: maximum recursion depth exceeded while calling a Python object

Python RecursionError: maximum recursion depth exceeded while calling a Python object

In programming, recursive functions are routines or methods that call themselves directly or indirectly. Python limits the number of times a recursion function can call itself. If the recursion function exceeds that limit, Python raises the error RecursionError: maximum recursion depth exceeded while calling a Python object .

This tutorial will discuss the above error in detail and help you learn to debug and resolve it. We will also walk through an example to understand this error.

Python RecursionError: Maximum Recursion Depth Exceeded while Calling a Python Object

Recursion is a powerful technique to solve specific types of problems in programming. However, one must be very careful while dealing with recursive functions, as they may enter infinite loops.

Every recursive function has a base case that acts as a terminating condition. When a function meets the base case, it stops calling itself and returns the value back.

However, if the base case is not defined correctly or the recursive logic is incorrect, the function may end up calling itself infinitely. This means the function continues calling itself without any termination condition.

Calling any function occupies space in the memory. And calling a function inside a function for infinite times can occupy almost every part of your computer memory. To tackle this problem, Python has implemented a Recursion Depth limit.

According to the Python recursion depth limit, by default, a recursion function can all itself only 1000 times. If the recursion exceeds this limit, the interpreter throws the error RecursionError: maximum recursion depth exceeded while calling a Python object .

To know the default Recursion limit for your program, you can use the getrecursionlimit() method from the Python sys modules.

import sys print("This default recursion limit is :", sys.getrecursionlimit())
This default recursion limit is : 1000 

If we look at the recursion error statement, we can divide it into two parts

1. RecursionError

RecursionError is one of the Python standard exceptions. It is a module exception that comes under the Python RuntimeError. Python raises this exception when it detects a maximum recursion depth in a program.

2. maximum recursion depth exceeded while calling a Python object

The " maximum recursion depth exceeded while calling a Python object " statement is the error message that tags along with the RecursionError exception. This error message tells us that a Python function has exceeded the number of recursion calls.

Common Example Scenario

Let us write a Python program using recursion that prints the nth number from a Fibonacci series. You can even write this program using the for loop.

In the Fibonacci series , the first two numbers are 0 and 1; the following numbers are calculated with the sum of the previous two numbers.

# recursive function to find the nth Fibonacci number def n_fibonacci(n): if n==0: return 0 elif n==1: return 1 else: return n_fibonacci(n-1)+n_fibonacci(n-2) # n=10 print(f"The st/nd/th Fibonacci number is: ",n_fibonacci(n-1))
The 10 st/nd/th Fibonacci number is: 34

The above program is correct, and it also shows the correct output. But if we change the value of n=10 to n=1005 , it will raise the error.

# recursive function to find the nth Fibonacci number def n_fibonacci(n): if n==0: return 0 elif n==1: return 1 else: return n_fibonacci(n-1)+n_fibonacci(n-2) #out of the recursion range n=1005 print(f"The st/nd/th Fibonacci number is: ",n_fibonacci(n-1))
RecursionError: maximum recursion depth exceeded in comparison

You can see that we receive the RecursionError with a different Error message. This is because the error message changes according to the operation we perform inside the function.

Here, it displays " maximum recursion depth exceeded in comparison ". This is because after exceeding the recursion limit, the Python interpreter can also not perform the comparison operator inside the recursion.

Solution

Python provides a setrecursionlimit() method that accepts an integer value as an argument and sets it as a recursion limit for the program. We can use this method to increase the default recursion depth limit.

Note: The setrecursionlimit() method is also limited and can only increase the recursion limit depth to 3500.

To solve the above example, we can increase the recursion limit to 2000 using the setrecursionlimit() method.

import sys # increase the recursion limit sys.setrecursionlimit(2000) # recursive function to find the nth fibonacci number def n_fibonacci(n): if n==0: return 0 elif n==1: return 1 else: return n_fibonacci(n-1)+n_fibonacci(n-2) #now in recursion range n=1005 print(f"the st/nd/th fibonacci number is: ",n_fibonacci(n-1))
the 1005 st/nd/th fibonacci number is: 482051511617926448416241857411039626258600330733909004920469712704382351844831823569922886993050824175326520025449797859766560885196970738202943545195859929088936259370887605815413541849563887924611727164704130

Executing the above program may take 10 to 20 minutes to finish because it calls the function repeatedly 2000 times.

Wrapping Up!

The RecursionError occurs when a recursion call exceeds the default recursion depth limit. When you encounter this error in your Python program, you must consider using an iterative approach to solve the problem. Using iterative statements like for and while loop, we can perform the desired action quickly and efficiently.

However, if you wish to solve your problem recursively, in that case, you can use them setrecursivelimit() to increase the default limit of the recursion call.

If you still get this error in your Python program, you can share your code in the comment section. We will try to help you with debugging.

People are also reading:

Источник

Читайте также:  Java switch case enumeration
Оцените статью