How to setup environment variables for `flask run` on Windows?
Solution 1
Windows PowerShell
set FLASK_APP=hello.py
$env:FLASK_APP = "hello.py"
flask run
Solution 2
you need to provide an application environment. So Flask needs to know, the .py
file to run.
Try to run that command
export FLASK_APP=application.py
where application.py
- name of your Flask app in my case it is application.py.
after that
flask run
Solution 3
I used this and it worked because I am doing it in Windows PowerShell.
$env:FLASK_APP = "app.py"
however, flask run
didn't and gave me could not import application.
Solution 4
hope this could help someone, first set flask env like this inside the python virtual env (for windows command prompt)
set FLASK_ENV=development
then
set FLASK_APP=app.py
(app.py should be your flask application file)
Solution 5
My error was also same but fortunately I was able to resolve it. Here you go,
D:\Development\Projects\Python_Projects\flask_blog>set FLASK_APP=app.py
D:\Development\Projects\Python_Projects\flask_blog>$env:FLASK_APP = "app.py"
D:\Development\Projects\Python_Projects\flask_blog>python -m flask run
Related videos on Youtube
user9818569
Updated on April 25, 2022Comments
-
user9818569 about 2 years
I just started learning flask and I am stuck at setting up the Flask environment variables. I don't know how to setup the environment variables. Whenever I use the
flask run
command, I encounter the following error:Error message : Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
I did a lot of google searches to setup environment variables on Windows but I am unable to find a solution and sometimes I am not able to understand the solution.
How to do this ? How to get the "app.py" or "wsgi.py" ?
-
Solaiman almost 6 yearsYou should post the structure of your code, that way people can help you faster. Anyway take a look over here: stackoverflow.com/questions/49238583/flask-run-vs-python
-
-
Kevin almost 5 yearsThanks for your answer. Can you provide some additional explanation or resources to improve your answer?
-
seghair tarek almost 5 yearsPut "set FLASK_APP = app.py" then "flask run" in the cmd command make shore you have installed flask with "pip install flask"
-
user3680029 over 2 yearsthat's the right way in 2021 :-)
-
Admin over 2 yearsYour answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-
Admin over 2 yearsYour answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-
Swapnadeep Mukherjee over 2 yearsThis is good for MacOS but the use case is in Windows. So, it will be
set FLASK_APP=application.py
-
BrianBeing about 2 yearsThis was the solution. Thank you!
-
ellhe-blaster about 2 yearsthe answer you are arguing can be supplemented with more information please add more information by editing and also read how to write a good answer in the help center