Unable start the Flask environment

Issue

This is a React project but I want to just test the Flask environment for now.

My directories:

api
   |app
       |_py_cache
       | .flaskenv
       | api.py
   |venv

api.py

  import time
  from flask import Flask

  app = Flask(__name__)

  @app.route('/time')
  def get_current_time():
       return {'time': time.time()}

.flaskenv

 FLASK_APP=api.py
 FLASK_ENV=development

package.json

  "scripts": {
"start": "react-scripts start",
"start-api": "cd api && venv\\Scripts\\activate && cd app && flask run --no-debugger",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
 },
 "proxy": "http://127.0.0.1:5000/"

After using flask run and going to the url, I get this error: flask.cli.NoAppException: Failed to find Flask application or factory in module "api". Use "FLASK_APP=api:name to specify one.

Solution

This is a common mistakes in beginners. You have to add the following code at the end of your code of api.py :

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

Or the best practice:

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

Answered By – Anandakrishnan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published