“运行时错误:生成器引发了停止迭代"每次我尝试运行应用程序时 [英] "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
我尝试了其他人的代码并且发生了完全相同的事情.此外,我尝试重新安装 web.py(experimental) 但它仍然不起作用.
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.
推荐答案
从文件路径判断,您运行的是 Python 3.7.如果是这样,你就会被 new-in-3.7 行为:
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:
Python 3.7 中的所有代码都启用了 PEP 479,这意味着在协程和生成器中直接或间接引发的 StopIteration 异常被转换为 RuntimeError 异常.(由 Yury Selivanov 在 bpo-32670 中贡献.)
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.)
在此更改之前,由生成器引发或通过的 StopIteration
简单地结束了生成器的使用寿命(异常被默默吞下).您正在使用的模块必须重新编码才能在 3.7 中按预期工作.
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.
他们可能需要改变:
yield next(seq)
到:
try:
yield next(seq)
except StopIteration:
return
这篇关于“运行时错误:生成器引发了停止迭代"每次我尝试运行应用程序时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!