简体   繁体   English

启动 Flask 应用程序时出错,出现错误“无法找到 Flask 应用程序”

[英]Error launching Flask app with error "Failed to find Flask application"

I'm new to Flask .我是Flask的新手。 To launch the flask app, I did python -m flask run but I repeatedly get the error:要启动 flask 应用程序,我执行了python -m flask run但我反复收到错误:

Failed to find Flask application or factory in module "app".在模块“app”中找不到 Flask 应用程序或工厂。 Use "FLASK_APP=app:name to specify one.使用“FLASK_APP=app:name 指定一个。

I am using a virtualenv on a Windows 10 machine我在 Windows 10 机器上使用virtualenv

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "Hello, World!"

if __name__ == '__main__':
    app.run(debug=True)

I expected a web server to start and to be able to navigate to localhost http://127.0.0.1:5000/ where I could view Hello, World!我希望 web 服务器能够启动并能够导航到本地主机http://127.0.0.1:5000/在那里我可以查看Hello, World! on my browser, but that never happened.在我的浏览器上,但这从未发生过。

Instead of just "flask" use FLASK_APP=theflaskapp.py , like what Marco suggested:而不是“烧瓶”使用FLASK_APP=theflaskapp.py ,就像 Marco 建议的那样:

 env FLASK_APP=theflaskapp.py python -m flask run

This should fix it, if not, make sure you are executing the command to run the script in the same directory as it.这应该可以修复它,如果没有,请确保您正在执行命令以在与其相同的目录中运行脚本。 You should also check if the problem is in flask or not by running "python theflaskapp.py" (In the same directory as the flask app still) and see if it works at all.您还应该通过运行“python theflaskapp.py”(仍在与烧瓶应用程序相同的目录中)检查问题是否在烧瓶中,并查看它是否有效。

Reproducing the problem, and fixing it重现问题并修复它

Hello, I have reproduced the problem This is the error code:你好,我已经重现了这个问题,这是错误代码:

Error: Failed to find Flask application or factory in module "src.app".错误:无法在模块“src.app”中找到 Flask 应用程序或工厂。 Use "FLASK_APP=src.app:name to specify one.使用 "FLASK_APP=src.app:name 指定一个。

Steps of reproducing the error:重现错误的步骤:

  1. Create a file called app.py创建一个名为app.py的文件
  2. inside app.py put this code:在 app.py 里面放这个代码:
from flask import Flask

def create_app():
    app = Flask("abc")

    @app.route('/')
    def hello_world():
        return 'Hello, World!'

Or let the file be empty, like this:或者让文件为空,如下所示:

# This is an empty file
# There is no flask application here
  1. Inside CLI run these commands:在 CLI 中运行以下命令:
export FLASK_APP=app.py
flask run
  1. watch the error appear on the screen观看错误出现在屏幕上

Solution 1 :解决方案 1:

  1. make the function return the app使函数返回应用程序
  2. Clearly create a variable called app and make it equal to the return value of the function, like this:清楚地创建一个名为app的变量,并使其等于函数的返回值,如下所示:
 from flask import Flask def create_app(): app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, World!' return app app = create_app()

Solution 2: Get the app outside of the function:解决方案 2:在函数之外获取应用程序:

 from flask import Flask app = Flask("abc") @app.route('/') def hello_world(): return 'Hello, World!'

solution for flask Error: Could not locate a Flask application.烧瓶错误的解决方案:找不到烧瓶应用程序。 You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.您没有提供“FLASK_APP”环境变量,并且在当前目录中找不到“wsgi.py”或“app.py”模块。 on windows when you tried set FLASKAPP=appname.py if your using PowerShell and if get the above error try this 👇👇在 Windows 上尝试set FLASKAPP=appname.py如果您使用 PowerShell 并且如果出现上述错误,请尝试此👇👇

$env:FLASK_APP = "filename"
$env:FLASK_ENV = "development"
Flask run

try it if your having trouble launching the flask app.如果您在启动烧瓶应用程序时遇到问题,请尝试一下。

You need to specify what file you want to work with.您需要指定要使用的文件。 Try this.尝试这个。

For Windows:对于 Windows:

set FLASK_APP=app.py
python -m flask run

For Mac OS and Linux:对于 Mac 操作系统和 Linux:

export FLASK_APP=app.py
python -m flask run

Another working option is executing this command instead of flask run :另一个工作选项是执行此命令而不是 flask run :

flask run --host=0.0.0.0

I had the same issue and I tried all the mentioned answers here but couldn't work finally this did the trick.我有同样的问题,我在这里尝试了所有提到的答案,但最终无法奏效。

I'm getting the same error.我遇到了同样的错误。 The problem is you have to set the environment variable FLASK_APP in current project.问题是您必须在当前项目中设置环境变量 FLASK_APP。

However, I have easiest way to solve this problem.但是,我有最简单的方法来解决这个问题。 Instead of using flask run command you can use python app.py .您可以使用python app.py而不是使用flask run命令。

"app.py" is your application entry point. “app.py”是您的应用程序入口点。

I had a similar problem.我有一个类似的问题。 It seems to work fine from the command prompt with python app.py .从命令提示符使用python app.py似乎可以正常工作。 However, it would fail to launch with VS Code Debug.但是,它无法使用 VS Code Debug 启动。

The trick was to add:诀窍是添加:

server = app.server

before calling the app.run_server() portion在调用app.run_server()部分之前

The simplest way to run your app without getting any issue is to create the app then run python app.py运行应用程序而不会出现任何问题的最简单方法是创建应用程序,然后运行python app.py

from flask import (
    Flask, 
    jsonify
)

# Function that create the app 
def create_app(test_config=None ):
    # create and configure the app
    app = Flask(__name__)

    # Simple route
    @app.route('/')
    def hello_world(): 
        return jsonify({
           "status": "success",
            "message": "Hello World!"
        }) 
     
    return app # do not foget to return the app

APP = create_app()

if __name__ == '__main__':
    # APP.run(host='0.0.0.0', port=5000, debug=True)
    APP.run(debug=True)

On venv and my Windows10 only this syntax works:在 venv 和我的 Windows10 上,只有这种语法有效:

set FLASK_APP=name_of_file:name_of_var_app
If file is application.py and 'app = Flask(__name__)',
run:
set FLASK_APP=application:app

You should change the name of the file, I have the same problem.你应该改变文件名,我也有同样的问题。 So, I changed the name of the app file and It works for me.所以,我更改了应用程序文件的名称,它对我有用。 At first my app file name is 'socket.py' and I change it to 'app.py'.起初我的应用程序文件名是“socket.py”,我将其更改为“app.py”。

我的问题是python文件没有执行权限:

chmod a+x app.py

Similar issue.类似的问题。 I was running the code in vs code.我在 vs 代码中运行代码。 Installed python extension and after selecting a different python interpreter( in my case 3.10,5 64 bit).安装 python 扩展并选择不同的 python 解释器(在我的情况下为 3.10,5 64 位)。 got a play button on the top right corner of my vs code interface.我的 vs 代码界面右上角有一个播放按钮。 That solved the issue for me.这为我解决了这个问题。

Instead of providing a super straightforward "try this" suggestion, I'd recommend going straight to the flask source code to where this exception gets thrown, and reverse engineer from there.我建议不要提供一个超级直接的“试试这个”建议,而是直接转到 flask 源代码到引发此异常的位置,然后从那里进行逆向工程。 https://github.com/pallets/flask/blob/main/src/flask/cli.py#L78-L82 https://github.com/pallets/flask/blob/main/src/flask/cli.py#L78-L82

You can open this in your editor by going to the site-packages/flask/cli.py file and setting breakpoints around where this error gets thrown, and poke around from there.您可以通过转到site-packages/flask/cli.py文件并在引发此错误的位置周围设置断点来在编辑器中打开它,然后从那里四处寻找。

In my case was a very simple issue, the activate.bat file was setup as Macintosh (CR) I changed to Windows (CR LF) and that worked for me.在我的情况下是一个非常简单的问题,activate.bat 文件设置为 Macintosh (CR) 我更改为 Windows (CR LF),这对我有用。

Explanation: In windows OS, for the instruction to take effect, for example [set FLASK_ENV = development] a strong carriage return is needed, such as the (CR LF), that is what I could verify in my case.说明:在 windows 操作系统中,为了使指令生效,例如 [set FLASK_ENV = development] 需要一个强回车,例如 (CR LF),这就是我可以验证的情况。 You can try to run the SET instruction in the console, cmd, and check if the FLASK settings are reflected.您可以尝试在控制台、cmd中运行SET指令,并检查是否反映了FLASK设置。 I understand that it is more a problem of the configuration of the environment and not of the application itself.我知道这更多是环境配置的问题,而不是应用程序本身的问题。

super simple in my case, and almost stupid...I had forgotten to save the file (CTRL+S), and I got exactly the same error.在我的情况下超级简单,而且几乎是愚蠢的......我忘记了保存文件(CTRL + S),我得到了完全相同的错误。 Once I saved the file, it worked directly!一旦我保存了文件,它就可以直接工作了!

尝试从这行代码中删除空格,当我遇到此错误时,这是​​为我解决的问题:

app=Flask(__name__)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:无法在模块“app”中找到 Flask 应用程序或工厂。 使用 'FLASK_APP=app:name' 指定一个 - Error: Failed to find Flask application or factory in module 'app'. Use 'FLASK_APP=app:name' to specify one Docker 运行错误 - 无法在模块“app”中找到 Flask 应用程序或工厂。 使用 "FLASK_APP=app:name 指定一个 - Docker run error - Failed to find Flask application or factory in module "app". Use "FLASK_APP=app:name to specify one 在flask中找不到flaskblog错误 - Failed to find the flaskblog error in flask Flask 应用程序出现“找不到 flask 应用程序。.....FLASK_APP 环境变量”错误 Flask 迁移 - Flask app getting error of "could not locate flask application. .....FLASK_APP environment variable" for Flask Migrate Flask shell 未能在模块“flask”中找到 Flask 应用程序或工厂 - Flask shell failed to to find Flask application or factory in module “flask” Flask 应用程序部署成功 - 应用程序错误 - Flask app deployed successfully - Application Error Heroku 部署 Flask 应用程序时出现“应用程序错误” - Heroku "Application error" when deploying Flask app 在模块“app”中找不到 Flask 应用程序或工厂。 使用 'FLASK_APP=app:name' 指定一个 - Failed to find Flask application or factory in module 'app'. Use 'FLASK_APP=app:name' to specify one 语法错误烧瓶应用 - Syntax error flask application 烧瓶应用程序中的导入错误 - Import Error in flask application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM