gpt4 book ai didi

python - Flask 错误 werkzeug 路由故障排除

转载 作者:行者123 更新时间:2023-12-02 03:15:52 26 4
gpt4 key购买 nike

我正在按照教程制作最简单的“Hello World”Flask 应用程序,如下所述: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

为了上下文,我在 C 驱动器的根目录下创建了目录,以避免潜在的路径问题。我正在运行 Python 3.8、Flask 1.0.2 和 werkzeug 0.15.2

在发出 Flask 命令之前,我做了两件事。创建虚拟环境并安装本地flask版本

$ python -m venv venv
$ venv\Scripts\activate
(venv) $ pip install flask

并设置 FLASK_APP 环境变量

(venv) $ set FLASK_APP=microblog.py

当我发出“flask run”命令时,会运行顶级脚本(此处的内容)

from app import app

它调用应用程序文件夹中的 __init__.py 文件,其中包含:

from flask import Flask

app = Flask(__name__) #The name "app" is being passed

from app import routes

第二行失败并显示以下错误输出:

Traceback (most recent call last):
File "C:\Python38\lib\runpy.py", line 192, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Python38\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\microblog\venv\Scripts\flask.exe\__main__.py", line 9, in <module>
File "c:\microblog\venv\lib\site-packages\flask\cli.py", line 894, in main
cli.main(args=args, prog_name=name)
File "c:\microblog\venv\lib\site-packages\flask\cli.py", line 557, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "c:\microblog\venv\lib\site-packages\click\core.py", line 717, in main
rv = self.invoke(ctx)
File "c:\microblog\venv\lib\site-packages\click\core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "c:\microblog\venv\lib\site-packages\click\core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "c:\microblog\venv\lib\site-packages\click\core.py", line 555, in invoke
return callback(*args, **kwargs)
File "c:\microblog\venv\lib\site-packages\click\decorators.py", line 64, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "c:\microblog\venv\lib\site-packages\click\core.py", line 555, in invoke
return callback(*args, **kwargs)
File "c:\microblog\venv\lib\site-packages\flask\cli.py", line 767, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File "c:\microblog\venv\lib\site-packages\flask\cli.py", line 293, in __init__
self._load_unlocked()
File "c:\microblog\venv\lib\site-packages\flask\cli.py", line 317, in _load_unlocked
self._app = rv = self.loader()
File "c:\microblog\venv\lib\site-packages\flask\cli.py", line 372, in load_app
app = locate_app(self, import_name, name)
File "c:\microblog\venv\lib\site-packages\flask\cli.py", line 235, in locate_app
__import__(module_name)
File "c:\microblog\microblog.py", line 1, in <module>
from app import app
File "c:\microblog\app\__init__.py", line 5, in <module>
app = Flask(__name__) #The name "app" is being passed
File "c:\microblog\venv\lib\site-packages\flask\app.py", line 558, in __init__
self.add_url_rule(
File "c:\microblog\venv\lib\site-packages\flask\app.py", line 66, in wrapper_func
return f(self, *args, **kwargs)
File "c:\microblog\venv\lib\site-packages\flask\app.py", line 1216, in add_url_rule
self.url_map.add(rule)
File "c:\microblog\venv\lib\site-packages\werkzeug\routing.py", line 1562, in add
rule.bind(self)
File "c:\microblog\venv\lib\site-packages\werkzeug\routing.py", line 711, in bind
self.compile()
File "c:\microblog\venv\lib\site-packages\werkzeug\routing.py", line 767, in compile
self._build = self._compile_builder(False)
File "c:\microblog\venv\lib\site-packages\werkzeug\routing.py", line 1128, in _compile_builder
return self.BuilderCompiler(self).compile(append_unknown)
File "c:\microblog\venv\lib\site-packages\werkzeug\routing.py", line 1119, in compile
co = types.CodeType(*code_args)
TypeError: code() takes at least 14 arguments (13 given)

除了下面的答案之外,请确保您的 PATH 变量设置正确。就我而言,我安装了 Anaconda 和常规 Python 安装,但两者存在版本冲突。最终,应用程序通过了Anaconda Prompt运行。要解决此类问题,请尝试将 bash_profile 中的 Anaconda 路径添加到 Windows 中的 PATH 变量

开瓶快乐:)!

最佳答案

看起来flask(werkzeug模块)与python3.8不兼容github issue如果您使用 werkzeug <0.15.5,请考虑降级到 python 3.7(并将虚拟环境中的 python3 链接指向 python3.7)或将 werkzeug 模块升级到 0.15.5 或更高版本。

关于python - Flask 错误 werkzeug 路由故障排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56127413/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com