81

I am trying to run this code in Python 3.7:

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.

smci
  • 32,567
  • 20
  • 113
  • 146
no4syn
  • 813
  • 1
  • 6
  • 4
  • Python 3.7 looping is now harder and requires a deeper understanding of the internals of python (generators). Java for-each is easier than python now. – barrypicker Dec 05 '19 at 00:00

7 Answers7

137

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
bryant1410
  • 5,540
  • 4
  • 39
  • 40
Tim Peters
  • 67,464
  • 13
  • 126
  • 132
  • 2
    So if this is in a 3rd party app you need to upgrade it? Are you SOL if that 3rd party app is not fixed for PEP 479? – Scott Skiles Mar 21 '19 at 17:21
  • 4
    Of course upgrading the app won't make a difference unless they've changed the app. If they haven't, you can patch it yourself. Or refrain from moving to Python 3.7 until the apps you care about do.comply with PEP 479. Since I've seen very few reports of this in the wild, I believe it's a rare problem. – Tim Peters Mar 21 '19 at 17:30
  • Great, thanks. Using a virtual env when Python 3.7 does not work sounds like a great solution when necessary. – Scott Skiles Mar 21 '19 at 17:33
  • Sorry for asking a noob question - is there any implications with this solution? Why is it okay to silently swallow the exception? – CozyAzure Oct 09 '19 at 10:48
  • 2
    @CozyAzure, read the PEP, which is the only justification for this change you'll' get. Before this change, `StopIteration` _was_ silently swallowed by generators, as the original generator PEP (255) specified must happen. So the suggested code merely restores the behavior generators had for years. – Tim Peters Oct 10 '19 at 03:00
  • How can I fix `yield from`? – Colonel Panic Nov 26 '19 at 08:22
  • @ColonelPanic, you know the drill here: what did you try? what happened? what do you want to happen instead? You should probably open a new, self-contained question. – Tim Peters Nov 26 '19 at 18:18
  • I upgraded mongoengine from 0.14.0 to 0.19.1 and `flask-mongoengine` to `0.9.5` – Ohad Perry Feb 09 '20 at 15:00
  • 1
    Another good solution, depending on the context, is passing a default value to next instead of a try-except block: `yield next(seq, None)`. – bryant1410 Jan 16 '21 at 19:49
  • @bryant1410: Yes [`yield next(seq, None)`](https://docs.python.org/3/library/functions.html?highlight=next#next) is much better, cleaner and more compact than this. Please post it as an answer. – smci Jul 23 '23 at 04:00
  • 1
    But note @bryant1410's "depending on the context". `yield next(seq, None)` has very different behavior, depending on context: it does _not_ end the containing generator's life, but yields an additional `Nene` value that `seq` itself did not deliver. Depending on context, that may be harmless - or it may be catastrophic. The given answer reproduces the 3.6 behavior regardless of context. – Tim Peters Jul 23 '23 at 04:14
3

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.

Leo Gomez
  • 41
  • 2
3

My solution was to upgrade these pips

mongoengine from 0.14.0 to 0.19.1 and

flask-mongoengine to 0.9.5

it worked.

WebQube
  • 8,510
  • 12
  • 51
  • 93
2

Most major packages have fixed this issue by now, but one major package that hasn't is the clips/pattern project. It hasn't been updated since August 2018, so it never received a fix.

Since this is the top Google result for "python pattern stopiteration", here's a workaround:

def pattern_stopiteration_workaround():
    try:
        print(lexeme('gave'))
    except:
        pass

def main():
    pattern_stopiteration_workaround()
    #Add your other code here

Basically, the pattern-related code will only fail the first time you run it, so you first need to run it once and catch the Exception that it throws.

It's worked well enough for my own scripts, but I don't know if it fixes every possible issue.

Ideally though, somebody should fork the clips/pattern project since it's no longer maintained.

Pikamander2
  • 7,332
  • 3
  • 48
  • 69
1

They fixed this issue, just uninstall your current web.py version and I got an error when running pip install web.py from windows 10. So I run the pip install -e git+https://github.com/webpy/webpy.git#egg=webpy command to get the latest version from master branch. This won't execute RuntimeError: generator raised StopIteration error as question mentioned.

Kushan Gunasekera
  • 7,268
  • 6
  • 44
  • 58
0

This should be fixed in #577: https://github.com/webpy/webpy/pull/577

-2

I was facing the same issue for below command

python setup.py test

Error solved when I upgraded the pytest version

pip uninstall pytest
pip install pytest
Sanket
  • 59
  • 5
  • In case you didn't know, **`pip install -U pytest`** will cause `pip` to do an upgrade; without the `-U` option it just says the package is already installed. – Alastair Irvine Dec 09 '22 at 12:08