Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Env. Variables not set while running Minimal Flask application

Tags:

python

flask

I am trying to follow the flask documentation on my windows machine given at the following link: http://flask.pocoo.org/docs/0.11/quickstart/#debug-mode

Firstly I wrote the code below in a python script:

from flask import Flask app = Flask(__name__)  @app.route("/") def hello():     return "Hello World!"  if __name__ == "__main__":     app.run(debug=True) 

I saved this in a file called run.py

Then wrote this command in the command window:

set FLASK_APP = run.py  flask run 

Upon running this, I am getting the following error:

"Error: Could not locate flask application. You did not provide the FLASK_APP environment variable"

I was expecting to get this instead:

Running on http://127.0.0.1:5000/ 

Can somebody tell me how to fix this?

like image 497
Batool Avatar asked Jun 15 '16 04:06

Batool


People also ask

How do you set environment variables in flask?

Command-line: Similarly, the FLASK_ENV variable sets the environment on which we want our flask application to run. For example, if we put FLASK_ENV=development the environment will be switched to development. By default, the environment is set to development.

Which environment variable Do you need to change in order to run flask?

New in version 1.0. The environment in which the Flask app runs is set by the FLASK_ENV environment variable. If not set it defaults to production . The other recognized environment is development .


2 Answers

I faced the same issue while using PowerShell and that fix worked for me:
instead of using set FLASK_APP = run.py, try $env:FLASK_APP = "run.py"

like image 193
Folky Avatar answered Oct 01 '22 15:10

Folky


If you are using powershell it does not works i dont know why please use cmd.exe since i use VScode editor it provide powershell as a terminal(ctrl + back-tick) by default so i was trying to run flask app on the powershell and it was giving me same response as you are getting

  1. open cmd.exe (or if you are VSCode user like me simply write cmd on that terminal, or you can also press Ctrl + Shift + C to open a windowed cmd terminal)

  2. set FLASK_APP=hello.py (without spaces, only for first run then it remember until restart of cmd)

  3. flask run (or just flask will also work)

note: this is only for windows user

like image 38
Inzamam Malik Avatar answered Oct 01 '22 15:10

Inzamam Malik