Python 每次我尝试运行应用程序时都会出现“RuntimeError:generator raise StopIteration”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51700960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"RuntimeError: generator raised StopIteration" every time I try to run app
提问by no4syn
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.
我尝试了其他人的代码并且发生了完全相同的事情。另外我尝试重新安装 web.py(experimental) 但它仍然没有工作。
回答by Tim Peters
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 described here:
从文件路径判断,您运行的似乎是 Python 3.7。如果是这样,您就会被此处描述的 3.7 新行为所吸引:
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.)
Python 3.7 中的所有代码都启用了 PEP 479,这意味着在协程和生成器中直接或间接引发的 StopIteration 异常被转换为 RuntimeError 异常。(由 Yury Selivanov 在 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.
在此更改之前,生成器StopIteration
引发或通过生成器只是结束了生成器的使用寿命(异常被默默吞下)。您正在使用的模块必须重新编码才能在 3.7 中按预期工作。
Chances are they'll need to change:
他们可能需要改变:
yield next(seq)
to:
到:
try:
yield next(seq)
except StopIteration:
return
回答by Leo Gomez
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.
因此,在我最近自学 Python 期间,有一门课程要求我安装 Web.py,但我收到了此错误,正如其中一个答案所述,必须更新它才能与 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.
我安装了pip3 install web.py==0.40-dev1
这个错误的包并开始在网上搜索解决方案。
What I did was search through webpy git and find the utils.pyfile 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.
我所做的是通过webpygit 搜索并在https://github.com/webpy/webpy/tree/master/web 中找到更新的utils.py文件,下载它,并用它来替换那个在我的 Lib/site-packages/web 文件夹中(我是 Windows 用户),它刚刚工作。
Hope this help someone.
希望这有助于某人。
回答by Kushan Gunasekera
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.
他们解决了这个问题,只需卸载您当前的web.py
版本,pip install web.py
从windows 10
. 所以我运行pip install -e git+https://github.com/webpy/webpy.git#egg=webpy
命令从master
分支获取最新版本。这不会RuntimeError: generator raised StopIteration
像提到的问题那样执行错误。
回答by WebQube
My solution was to upgrade these pips
我的解决方案是升级这些点数
mongoengine
from 0.14.0
to 0.19.1
and
mongoengine
从0.14.0
到0.19.1
和
flask-mongoengine
to 0.9.5
flask-mongoengine
到 0.9.5
it worked.
有效。
回答by Zhang Huangbin
This should be fixed in #577: https://github.com/webpy/webpy/pull/577
这应该在 #577 中修复:https: //github.com/webpy/webpy/pull/577