Env. Variables not set while running Minimal Flask application
Question:
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?
Answers:
Your code is correct. Try setting the Command-Line variable like this :
setx FLASK_APP run.py
And then run it : flask run
.
I fixed the same problem by closing the command window and restart it.
You have to set the FLASK_APP variable outside of the virtual environment. I’m assuming the reason that it worked after @Fang restarted the command window is because that action automatically exited the virtual environment.
So, if you’re in the virtual environment (indicated by a (venv)
at the start of line on command window), then type the following commands:
deactivate
set FLASK_APP=run.py
venvscriptsactivate
flask run
Did you run your terminal as administrator? I had the same problem, and running cmd/PowerShell as admin fixed it for me.
Edit: I spoke too soon. Running as admin did nothing.
The real answer (to my problem, at least) is a combination of some of the other answers.
- Instead of using
set FLASK_APP = myApp.py
, use setx FLASK_APP myApp.py
- Restart the terminal, and FLASK_APP will have the new value
export FLASK_APP=run.py
flask run --host=0.0.0.0
then can run normally, output something like:
* Serving Flask app "hello"
* Forcing debug mode on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 353-795-063
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"
Success with setx FLASK_APP test.py
. Note: if not run, close command line and start other one.
D:projectsflask-oauthlibpy2>.evnScriptsactivate
(.evn) D:projectsflask-oauthlibpy2>setx FLASK_APP test.py
SUCCESS: Specified value was saved.
(.evn) D:projectsflask-oauthlibpy2>flask run
* Serving Flask app "flask-oauthlibpy2.test"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [24/May/2017 14:35:06] "GET / HTTP/1.1" 200 -
Just drop the space around ‘=’ !
After half an hour searching for solution, I finally get what does ‘You did not provide the FLASK_APP environment variable’ mean. Because what I set is ‘FLASK_APP ‘ variable (with space).
had a similar problem. got resolved by using the following:
flask run –no-reload
learnt this from flask documentation http://flask.pocoo.org/docs/0.12/server/
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
-
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)
-
set FLASK_APP=hello.py
(without spaces, only for first run then it remember until restart of cmd)
-
flask run
(or just flask will also work)
note: this is only for windows user
I had got the same issue while running the first hello world for flask.
Ideally there should not be any space between
so use
set FLASK_APP=run.py
I was having this same problem running the commands as stated in Flask quick start. using Powershell on windows 10
projects>set export FLASK_APP=hello.py
projects>flask run
* Running on http://127.0.0.1:5000/
I needed to type python <filename.py>
while being in the directory containing the file with
from flask import Flask
app = Flask(name)
@app.route('/')
def hello_world():
return 'Hello, World!
after that you will get a message with an IP address in it
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
you copy IP address into your browser and your to that point you should see your hello world statement then.
I wonder why this is missing from the answer.
Here is a simple solution:
add app.run(port=5000)
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
app.run(port=5000)
and then in the same directory run the command
python run.app
Just drop the space around equal to sign.
set FLASK_APP=python_file_name.py
I was having the same problem, but the following command worked for me in PowerShell:
python -m flask run
I had the same issue on windows and found a solution from the flask documentation: https://flask.palletsprojects.com/en/1.1.x/tutorial/factory/
My file structure:
Flask_folder>>app
(NB! app folder contains: __init_.py and routes.py)
> set FLASK_APP=app
> set FLASK_ENV=development
> flask run
Here is what worked for me:
- Step 1: Open your CMD Command prompt
- Step 2: Go on the same directory in which my python file was on.
- Step 3: in the CMD command prompt run the command
setx FLASK_APP sqlAlchemy.py
- Step 4: close terminal
- Step 5: In the CMD command prompt run the command
python - m flask run
I have an Alienware laptop and am using Windows 10.
Not sure it worked but it was a combination of trial and ever.
Depending on the OS you have the solution will vary it seems.
IT could be an environmental variable issue but again not sure.
–
Here are other possible solutions that didn’t work for me but I gathered them up as possibilities.
- OPTION 2
- set FLASK_APP=sqlalchemy.py
- flask run
- OPTION 3
- set FLASK_APP=app
- set FLASK_ENV=development
- flask run
Hope this helps.
Different shell programs have different ways to set the environment variable in it. So that Flask can discover you application correctly.
-
Unix Bash/Zsh (Linux, Mac, etc.):
$ export FLASK_APP=hello
$ flask run
-
Windows CMD:
> set FLASK_APP=hello
> flask run
-
Windows PowerShell:
> $env:FLASK_APP = "hello"
> flask run
You can find more infomation in the Flask docs.
> set FLASK_APP=Your_file_name.py
> set FLASK_ENV=development
> flask run
it must be work for windows user only
You should leave a space between the equal sign and the file name like this in PowerShell in Windows.
set FLASK_APP= market.py
this worked for me.
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?
Your code is correct. Try setting the Command-Line variable like this :
setx FLASK_APP run.py
And then run it : flask run
.
I fixed the same problem by closing the command window and restart it.
You have to set the FLASK_APP variable outside of the virtual environment. I’m assuming the reason that it worked after @Fang restarted the command window is because that action automatically exited the virtual environment.
So, if you’re in the virtual environment (indicated by a (venv)
at the start of line on command window), then type the following commands:
deactivate
set FLASK_APP=run.py
venvscriptsactivate
flask run
Did you run your terminal as administrator? I had the same problem, and running cmd/PowerShell as admin fixed it for me.
Edit: I spoke too soon. Running as admin did nothing.
The real answer (to my problem, at least) is a combination of some of the other answers.
- Instead of using
set FLASK_APP = myApp.py
, usesetx FLASK_APP myApp.py
- Restart the terminal, and FLASK_APP will have the new value
export FLASK_APP=run.py
flask run --host=0.0.0.0
then can run normally, output something like:
* Serving Flask app "hello"
* Forcing debug mode on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 353-795-063
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"
Success with setx FLASK_APP test.py
. Note: if not run, close command line and start other one.
D:projectsflask-oauthlibpy2>.evnScriptsactivate
(.evn) D:projectsflask-oauthlibpy2>setx FLASK_APP test.py
SUCCESS: Specified value was saved.
(.evn) D:projectsflask-oauthlibpy2>flask run
* Serving Flask app "flask-oauthlibpy2.test"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [24/May/2017 14:35:06] "GET / HTTP/1.1" 200 -
Just drop the space around ‘=’ !
After half an hour searching for solution, I finally get what does ‘You did not provide the FLASK_APP environment variable’ mean. Because what I set is ‘FLASK_APP ‘ variable (with space).
had a similar problem. got resolved by using the following:
flask run –no-reload
learnt this from flask documentation http://flask.pocoo.org/docs/0.12/server/
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
-
open cmd.exe (or if you are VSCode user like me simply write
cmd
on that terminal, or you can also pressCtrl + Shift + C
to open a windowed cmd terminal) -
set FLASK_APP=hello.py
(without spaces, only for first run then it remember until restart of cmd) -
flask run
(or just flask will also work)
note: this is only for windows user
I had got the same issue while running the first hello world for flask.
Ideally there should not be any space between
so use
set FLASK_APP=run.py
I was having this same problem running the commands as stated in Flask quick start. using Powershell on windows 10
projects>set export FLASK_APP=hello.py
projects>flask run
* Running on http://127.0.0.1:5000/
I needed to type python <filename.py>
while being in the directory containing the file with
from flask import Flask
app = Flask(name)
@app.route('/')
def hello_world():
return 'Hello, World!
after that you will get a message with an IP address in it
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
you copy IP address into your browser and your to that point you should see your hello world statement then.
I wonder why this is missing from the answer.
Here is a simple solution:
add app.run(port=5000)
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
app.run(port=5000)
and then in the same directory run the command
python run.app
Just drop the space around equal to sign.
set FLASK_APP=python_file_name.py
I was having the same problem, but the following command worked for me in PowerShell:
python -m flask run
I had the same issue on windows and found a solution from the flask documentation: https://flask.palletsprojects.com/en/1.1.x/tutorial/factory/
My file structure:
Flask_folder>>app
(NB! app folder contains: __init_.py and routes.py)
> set FLASK_APP=app
> set FLASK_ENV=development
> flask run
Here is what worked for me:
- Step 1: Open your CMD Command prompt
- Step 2: Go on the same directory in which my python file was on.
- Step 3: in the CMD command prompt run the command
setx FLASK_APP sqlAlchemy.py
- Step 4: close terminal
- Step 5: In the CMD command prompt run the command
python - m flask run
I have an Alienware laptop and am using Windows 10.
Not sure it worked but it was a combination of trial and ever.
Depending on the OS you have the solution will vary it seems.
IT could be an environmental variable issue but again not sure.
–
Here are other possible solutions that didn’t work for me but I gathered them up as possibilities.
- OPTION 2
- set FLASK_APP=sqlalchemy.py
- flask run
- OPTION 3
- set FLASK_APP=app
- set FLASK_ENV=development
- flask run
Hope this helps.
- flask run
Different shell programs have different ways to set the environment variable in it. So that Flask can discover you application correctly.
-
Unix Bash/Zsh (Linux, Mac, etc.):
$ export FLASK_APP=hello $ flask run
-
Windows CMD:
> set FLASK_APP=hello > flask run
-
Windows PowerShell:
> $env:FLASK_APP = "hello" > flask run
You can find more infomation in the Flask docs.
> set FLASK_APP=Your_file_name.py
> set FLASK_ENV=development
> flask run
it must be work for windows user only
You should leave a space between the equal sign and the file name like this in PowerShell in Windows.
set FLASK_APP= market.py
this worked for me.