简体   繁体   中英

flask.cli.NoAppException: Failed to find Flask application or factory in module "flaskr". Use "FLASK_APP=flaskr:name to specify one

I am building a python web application using flask and following tutorial from http://flask.pocoo.org/docs/1.0/tutorial/factory/ . I am running the flask from the parent directory but getting following error.

(venv) E:\python-code\python-web>flask run
 * Serving Flask app "flaskr" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 294-690-396
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [16/Mar/2019 13:10:43] "GET /hello HTTP/1.1" 500 -
Traceback (most recent call last):
  File "e:\python-code\python-web\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "e:\python-code\python-web\venv\lib\site-packages\flask\cli.py", line 95, in find_best_app
    module=module.__name__
flask.cli.NoAppException: Failed to find Flask application or factory in module "flaskr". Use "FLASK_APP=flaskr:name to specify one.
127.0.0.1 - - [16/Mar/2019 13:10:43] "GET /hello?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [16/Mar/2019 13:10:43] "GET /hello?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [16/Mar/2019 13:10:43] "GET /hello?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [16/Mar/2019 13:10:43] "GET /hello?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -
127.0.0.1 - - [16/Mar/2019 13:10:44] "GET /hello?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [16/Mar/2019 13:10:44] "GET /hello?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -

Please help.

I have tried Running Flask app from cli gives "no module named flaskr" and flask.cli.NoAppException: Failed to find application in module?

I have found my problem. I was missing return app as the last statement in my code. Sorry for confusion and apologies.

I got that error

flask.cli.NoAppException flask.cli.NoAppException: Failed to find Flask application or factory in module "flaskr". Use "FLASK_APP=flaskr:name to specify one.

I tried all answers, and it got resolved after I changed the function name from create_function to create_app

is your application dir not named properly? for example, if you wrote FLASK_APP=flaskr but your project name is something else, you will get this error. Also, what is in your flaskr/flaskr/__init__.py file if the project is named flaskr like in the tutorial?

I followed the instructions just like the official tutorial said but still got the same error.

My problem was with the IDE that I was using ( Pycharm )

Even though I had an init .py file in the flaskr directory, it still wasn't considered as a package.

So I deleted the flaskr directory and created a "new package". Then just copy the code in the init .py and that'll solve the problem

You can't name the FLASK_APP enviroment var as Flask.py. Try with another name please and try again.

No puedes poner el nombre de la variable de ambiente FLASK_APP como Flask.py. Prueba cambiando el nombre del archivo.

Depending on the OS you are using you need to export flaskr in your environment

For Linux and Mac:

export FLASK_APP=flaskr
export FLASK_ENV=development
flask run

For Windows cmd, use set instead of export:

set FLASK_APP=flaskr
set FLASK_ENV=development
flask run

For Windows PowerShell, use $env: instead of export:

$env:FLASK_APP = "flaskr"
$env:FLASK_ENV = "development"
flask run```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM