Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"RuntimeError: generator raised StopIteration" every time I try to run app

I am trying to run this code:

import web  urls = (     '/', 'index' )  if __name__ == "__main__":     app = web.application(urls, globals())     app.run() 

But it gives me this error everytime

C:\Users\aidke\Desktop>python app.py Traceback (most recent call last):   File "C:\Users\aidke\AppData\Local\Programs\Python\Python37-32\lib\site-packages\web\utils.py", line 526, in take     yield next(seq) StopIteration  The above exception was the direct cause of the following exception:  Traceback (most recent call last):   File "app.py", line 14, in <module>     app = web.application(urls, globals())   File "C:\Users\aidke\AppData\Local\Programs\Python\Python37-32\lib\site-packages\web\application.py", line 62, in __init__     self.init_mapping(mapping)   File "C:\Users\aidke\AppData\Local\Programs\Python\Python37-32\lib\site-packages\web\application.py", line 130, in init_mapping     self.mapping = list(utils.group(mapping, 2))   File "C:\Users\aidke\AppData\Local\Programs\Python\Python37-32\lib\site-packages\web\utils.py", line 531, in group     x = list(take(seq, size)) RuntimeError: generator raised StopIteration 

I tried someone else's code and the exact same thing happened. Additionally I tried reinstalling web.py(experimental) but it still didn't work.

like image 342
no4syn Avatar asked Aug 06 '18 05:08

no4syn


People also ask

How do you handle a StopIteration error?

We can catch the StopIteration exception by writing the code inside the try block and catching the exception using the 'except' keyword and printing it on screen using the 'print' keyword.

What is StopIteration error in Python?

In Python, StopIteration is an exception which occurred by built-in next() and __next__() method in iterator to signal that iteration is done for all items and no more to left to iterate.


2 Answers

To judge from the file paths, it looks like you're running Python 3.7. If so, you're getting caught by new-in-3.7 behavior:

PEP 479 is enabled for all code in Python 3.7, meaning that StopIteration exceptions raised directly or indirectly in coroutines and generators are transformed into RuntimeError exceptions. (Contributed by Yury Selivanov in bpo-32670.)

Before this change, a StopIteration raised by, or passing through, a generator simply ended the generator's useful life (the exception was silently swallowed). The module you're using will have to be recoded to work as intended with 3.7.

Chances are they'll need to change:

yield next(seq) 

to:

try:     yield next(seq) except StopIteration:     return 
like image 158
Tim Peters Avatar answered Oct 07 '22 23:10

Tim Peters


So during my recent self-learning on Python, a course required me to install Web.py and I was getting this error and as one of the answer stated, it had to be updated to be compatible with Python 3.7.

I installed the package with pip3 install web.py==0.40-dev1 ran into this error and started searching the web for a solution.

What I did was search through webpy git and find the utils.py file that was more recent in https://github.com/webpy/webpy/tree/master/web, downloaded it, and used it to replace the one that was in my Lib/site-packages/web folder (I'm a Windows user) and it just worked.

Hope this help someone.

like image 38
Leo Gomez Avatar answered Oct 08 '22 00:10

Leo Gomez