Solving Python RuntimeError: generator raised StopIteration

RuntimeError: generator raised StopIteration is an error which occurs in Python when you are not correctly using StopIteration.

In this article we are going to explain why the error is popping up and show you how to solve the error and get rid of it for good.

Exploring the RuntimeError: generator raised StopIteration

The RuntimeError: generator raised StopIteration is an error which occurs in Python when you are not correctly using StopIteration.

The message of the error should look like the error message bellow. This is important to avoid any kind of confusion between errors and solutions.

                                                                       #
Traceback (most recent call last):
  File "test.py", line 14, in <module>
    app = web.application(urls, globals())
.....
.....
    x = list(take(seq, size))
RuntimeError: generator raised StopIteration
                                                                       #

Bellow I make my best attempt at solving the error and present multiple possible solutions.

Solution 1 : for flask and mongoengine users.

If you are not using flask and mongoengine just skip this solution and try the solutions bellow.

The solution is actually simple, you should upgrade the mongoengine and flask-mongoengine pips.

I will show you how to do that for mongoengine and you can figure out flask-mongoengine easily.

First, remove pymongo

                                                                       #
pip uninstall pymongo
                                                                       #

Second, remove mongoengine

                                                                       #
pip uninstall mongoengine
                                                                       #

Then install pymongo

                                                                       #
pip install pymongo==2.8
                                                                       #

Finally, install mongoengine

                                                                       #
pip install mongoengine
                                                                       #

I hope this has helped you, alternatively jump into the next solution.

Solution 2 : use except StopIteration:

The second solution is simple, you should use StopIteration in your code.

as a result you should use.

                                                                       #
try:
    yield next(seq)
except StopIteration:
    return
                                                                       #

Instead of using yield next(seq) only.

Solution 3 : for web.py users.

A lot of people who have installed Web.py get this error. The solution is to upgrade Web.py since it is an incompatibility problem with python.

The command bellow does not solve the problem for a lot of people.

                                                                       #
pip3 install web.py==0.40-dev1
                                                                       #

A final option is to head to https://github.com/webpy/webpy/tree/master/web, download utils.py. And replace it inside Lib/site-packages/web.

The error above was hard to deal with, I spent hours looking for a proper solution or set of solutions.

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up

This error is solved guys, thank you for reading our article, if you have any other errors make sure to search in our site for a solution, so far we are providing solutions for errors in Python, cheers and keep coding.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/