40

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?

6
  • That is the exact example provided in the Flask documentation. If it is not working, it is likely a bug in Flask itself. Contact the author. Commented Jun 15, 2016 at 15:03
  • @RemyLebeau I managed to fix the error by writing <python -m run.py> in cmd to run the file instead of <python run.py>. By doing this, I am now getting another error that says "Error while finding spec for 'run.py' << class AttributeError'>: module 'run' has no attribute '-path-'>". Can you please offer some help?
    – Batool
    Commented Jun 18, 2016 at 19:43
  • see Python3 submodules setup does not update paths when run with -m switch, and note the comment about "the -m switch takes a python identifier, not a filename". Commented Jun 18, 2016 at 19:48
  • I got lost with the second link, but as of the first link to Flask Documentation that you had provided earlier, I ran exactly what has been mentioned export FLASK_APP = run.py flask run, but am getting the following error "Could not locate flask application. You did not provide the FLASK_app evironment variable". Will you be able to guide on this?
    – Batool
    Commented Jun 18, 2016 at 20:40
  • Sorry, this is as far as I can go. I don't use Python. Commented Jun 18, 2016 at 20:52

20 Answers 20

93

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"

8
  • 3
    This solution works for my case when using PowerShell. Thank you!
    – Lavande
    Commented Apr 19, 2017 at 3:40
  • 1
    Thanks for this solution. Experienced exact same problem (used Powershell instead of cmd) as the question starter and this solution works perfectly.
    – addicted
    Commented Oct 2, 2017 at 8:06
  • Could someone tell me how do I save this so I don't need to type it in each time I want to run my Flask application when I shut down my computer? Commented Feb 13, 2018 at 22:31
  • @CoreyLevinson There are some tips in the flask documentation: Environment Variables From dotenv, Environment Variables From virtualenv
    – Folky
    Commented Feb 14, 2018 at 6:25
  • I know it's old but worked for me. I'm using Visual Studio Code x64 on Windows 10, running powershell in the terminal. Commented Mar 22, 2018 at 16:55
19

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

9
  1. export FLASK_APP=run.py
  2. 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
8

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
4

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.

3

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

venv\scripts\activate

flask run
3

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).

1
  • 1
    This describes and fixes exactly OP's problem and should IMHO be the selected answer. Running powershell to run a python script is overkill...
    – Jeronimo
    Commented Aug 21, 2018 at 7:20
2

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.

  1. Instead of using set FLASK_APP = myApp.py, use setx FLASK_APP myApp.py
  2. Restart the terminal, and FLASK_APP will have the new value
2

I was having the same problem, but the following command worked for me in PowerShell:

python -m flask run
1

Your code is correct. Try setting the Command-Line variable like this :

setx FLASK_APP run.py

And then run it : flask run.

1

Success with setx FLASK_APP test.py . Note: if not run, close command line and start other one.

D:\projects\flask-oauthlibpy2>.evn\Scripts\activate

(.evn) D:\projects\flask-oauthlibpy2>setx FLASK_APP test.py

SUCCESS: Specified value was saved.

(.evn) D:\projects\flask-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 -
1

Just drop the space around equal to sign. set FLASK_APP=python_file_name.py

1

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

0

I fixed the same problem by closing the command window and restart it.

0

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/

0

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

0

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.
0
    > set FLASK_APP=Your_file_name.py
    > set FLASK_ENV=development
    > flask run

it must be work for windows user only

0

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.

-1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.