from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
我是 Flask 的新手。我编写了这段基本代码并将其保存在我的 D:\Cat_vs_Dog\scripts 文件夹中的 hello.py 中。
然后在命令提示符下,我写了以下命令。
C:\Users\Ketan Ingale>set FLASK_APP=D:\Cat_vs_Dog\scripts\hello.py
C:\Users\Ketan Ingale>flask run
* Serving Flask app "D:\Cat_vs_Dog\scripts\hello.py"
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
Usage: flask run [OPTIONS]
Error: Could not import "D".
我遇到了这个错误。我该怎么办...?
最佳答案
flask v1.1.x
https://flask.palletsprojects.com/en/1.1.x/cli/#application-discovery
以上文档链接描述:
The flask command is installed by Flask, not your application; it must be told where to find your application in order to use it. The FLASK_APP environment variable is used to specify how to load the application.
删除目录并只使用文件名并重试。
您可能需要 CD
到包含您的文件的目录。
flask 2.2.x
https://flask.palletsprojects.com/en/2.2.x/cli/#application-discovery
上面的文档链接指定:
The flask command is installed by Flask, not your application; it must be told where to find your application in order to use it. The
--app
option is used to specify how to load the application.
还有:
If --app is not set, the command will try to import “app” or “wsgi” (as a “.py” file, or package) and try to detect an application instance or factory.
关于python - 无法导入 "D": FLASK_APP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52581576/