How to setup environment variables for `flask run` on Windows?

82,123

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
Share:
82,123

Related videos on Youtube

user9818569
Author by

user9818569

Updated on April 25, 2022

Comments

  • user9818569
    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" ?

    error message

  • Kevin
    Kevin almost 5 years
    Thanks for your answer. Can you provide some additional explanation or resources to improve your answer?
  • seghair tarek
    seghair tarek almost 5 years
    Put "set FLASK_APP = app.py" then "flask run" in the cmd command make shore you have installed flask with "pip install flask"
  • user3680029
    user3680029 over 2 years
    that's the right way in 2021 :-)
  • Admin
    Admin over 2 years
    Your 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
    Admin over 2 years
    Your 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
    Swapnadeep Mukherjee over 2 years
    This is good for MacOS but the use case is in Windows. So, it will be set FLASK_APP=application.py
  • BrianBeing
    BrianBeing about 2 years
    This was the solution. Thank you!
  • ellhe-blaster
    ellhe-blaster about 2 years
    the 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