简体   繁体   中英

Error: Failed to find Flask application or factory in module 'app'. Use 'FLASK_APP=app:name' to specify one

I know this question was asked earlier as well, but since I could not find a solution, asking again.

I am trying to setup a Flask project on my local MacBook, but constantly getting the error -

Error: Failed to find Flask application or factory in module 'app'. Use 'FLASK_APP=app:name' to specify one

My Python version is - 3.9.5, Flask version is - 2.0.1, OS - macOS Catalina 10.15.7, Editor - Visual Studio Code

I am already using a separate Virtual environment for this particular project.

I have tried below methods to fix this issue:-

  1. python3 -m flask run --host=0.0.0.0

python3 -m flask run --host=0.0.0.0 /Users/macuser/.pyenv/versions/3.9.5/bin/python: No module named flask

  1. python -m flask run --host=0.0.0.0

python -m flask run --host=0.0.0.0 * Serving Flask app 'app.py' (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: on Usage: python -m flask run [OPTIONS] Try 'python -m flask run --help' for help.

Error: Failed to find Flask application or factory in module 'app'. Use 'FLASK_APP=app:name' to specify one.

  1. export FLASK_APP=app.py, flask run
  • Serving Flask app 'app.py' (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: on Usage: flask run [OPTIONS] Try 'flask run --help' for help.

Error: Failed to find Flask application or factory in module 'app'. Use 'FLASK_APP=app:name' to specify one.

This is my sys.path

['', '/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python39.zip', '/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9', '/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload', '/Users/macuser/projects/steve_env/lib/python3.9/site-packages']

Can someone please help me with the solution.

使用以下命令指定您的应用程序名称: export FLASK_APP=MY_APP.py

Had the same issue while debugging via vscode, app definition was like this

if __name__ == "__main__":
    app = create_app()
    app.run(host="127.0.0.1", port=5000, debug=True)

In my case, removing the if condition resolved it

app = create_app()
app.run(host="127.0.0.1", port=5000, debug=True)

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