无法在Windows 10的Git-Bash中为Python文档运行Flask

问题描述

我在按作业要求运行烧瓶时遇到问题。我的任务陈述如下:

enter image description here

我正在尝试通过git-bash进行尝试,并且还尝试通过Windows Powershell进行了同样的错误操作。我已经引用了stackflow文章,其中指出可以使用export FLASK_APP=app.py作为代码行,但是还没有走运。我需要重新安装Flask吗?不知道去哪里或做什么。我的计算机软件是Windows10。以下是我尝试在Git-Bash中运行flask以及我的app.py文档中的代码

enter image description here

enter image description here

解决方法

在文件中,您从未真正告诉Flask运行,仅是为了初始化。您想要这样的东西:

from flask import Flask
app = Flask(__name__)

if __name__ == '__main__': # Run flask when the file is called
    app.run(host="0.0.0.0",port=5000)

这将使烧瓶运行,并且host="0.0.0.0"将使其可被您的计算机访问。我不知道WSL是否转发它的端口,但是如果它转发了,则该应用程序应该在浏览器的localhost:5000上可用(尽管不会发生什么,因为这是一个空项目)。

您可以通过将该示例修改为:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    """Is called on the homepage"""
    return "Hello"

if __name__ == '__main__': # Run flask when the file is called
    app.run(host="0.0.0.0",port=5000)

然后在浏览器中转到localhost:5000,它应该显示“ Hello”