38

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

1

26 Answers 26

65

Windows PowerShell

set FLASK_APP=hello.py
$env:FLASK_APP = "hello.py"
flask run
0
24

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
1
  • 3
    This is good for MacOS but the use case is in Windows. So, it will be set FLASK_APP=application.py Jan 7, 2022 at 14:13
14

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.

1
  • that's the right way in 2021 :-) Sep 28, 2021 at 14:54
12

My error was also same but fortunately I was able to resolve it. Here you go,

set FLASK_APP=app.py
$env:FLASK_APP = "app.py"
python -m  flask run
10

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)

5

I don't think the 'flask run' command is the one which causes the error here. I got the same message error and the problem came from the fact I copied/pasted the set FLASK_APP and $env: FLASK_APP commands as written in the documentation. I had to add spaces before and after '>' or '=', and then everything worked.

Example: this command didn't work 'C:\path\to\app>set FLASK_APP=hello.py', but this one did 'C:\path\to\app > set FLASK_APP = hello.py'.

Maybe it's the same problem you have?

5

I was facing the same problem and the thing that worked for me was don't put spaces before and after the = sign.
For example: FLASK_APP = flaskblog.py is wrong and will likely give you an error because of the spaces before and after the = sign.
Instead try FLASK_APP=flaskblog.py
It worked for me.

0
3

You need to actually run it from your Windows command line, NOT the built in command line for something like Visual Studio Code. Run those commands from your windows command line, in the proper directory, and everything should work.

The reason this creates problems - Visual Studio Code creates a Powershell environment for your command line. You could use the recommended $env:FLASK_APP = "your_app.py" from within the VSC environment and that will work too.

A bit late but I hope this helps others!!!

1
  • This was the solution. Thank you!
    – BrianBeing
    Mar 20, 2022 at 16:45
3

You can try this

set FLASK_APP=application.py
flask run
2
  • 1
    Thanks for your answer. Can you provide some additional explanation or resources to improve your answer?
    – Kevin
    Jun 14, 2019 at 18:36
  • 1
    Put "set FLASK_APP = app.py" then "flask run" in the cmd command make shore you have installed flask with "pip install flask" Jun 15, 2019 at 23:07
2

If you're using powershell, make sure you add quotations when setting the environment variable:

$env:FLASK_APP = "app.py

Then flask run should work.

1

A step-wise solution is provided below:

  1. Go to the folder where you have placed your flask app (on the command line)

  2. Create a virtual environment as using the command ($ py -m venv env) here 'venv' is the short form of the virtual environment and 'env' at the end represents the name of the environment which you want (I have named it as env). Thereafter you can see at from the file explorer that a folder named 'env' is created in the folder stated at point #1 above.

  3. Enter the following command ($env\Scripts\activate) by pressing enter this will turn on your virtual environment

  4. Thereafter, enter the following command ($set FLASK_APP=<your app name>.py)

  5. Enter the following command ($flask run)

1

The similar error was appearing when I was trying to run the application. I have to change the path. I changed directory to the folder where hello.py was saved.

(venv) C:\Users\win10\flask-tutorial\myproject>cd venv
(venv) C:\Users\win10\flask-tutorial\myproject\venv>set FLASK_APP=hello.py
(venv) C:\Users\win10\flask-tutorial\myproject\venv>flask run
1

The set command works but to setup the environment, you need to make sure that you are in the right directory where the file is located. For example, if my application is hello_world.py and it is located at the venv\hello\hello_world.py, you need to make sure that you are in the right directory before setting up set FLASK_APP=hello_world.py this is for windows but in another OS, you need to use export instead set

(venv) C:\Users\myProjects\venv\hello\set FLASK_APP=hello_world.py

1

You're typing in the commands that look correct. It may be a Windows security item that your user cannot make changes to environmental variables. If you're on Windows 10, search "View Advanced System Settings." Then click environmental variables, hit new user variable and make it FLASK_APP and set the path where it asks. Then do flask run in terminal.

To add: if your terminal is just not cooperating with your environment variables, you can just call the method inside the script itself, avoiding this error completely.

from flask import Flask
app = Flask(__name__)

#your decorators, etc here

if __name__=='__main__':
    app.run()

Then when you're in your activated virtual environment, all that's needed is

$python flaskblog.py
1

I tried a few ways mentioned in this thread so thanks everyone for your inputs.This is what worked for me.

1st, making sure you're running it when you're on the main .py file;

2nd, if you're on a Windows, use set FLASK_APP="your_file.py" (don't forget the quotation marks)

and then flask db init to initialize

...woo so happy my app finally worked!

1

I resolved similar problem by running the Command Prompt with admin rights (Windows+R --> enter cmd --> Hold the keys Ctrl+Shift+Enter) and then running the below command:

set FLASK_APP=<ProgramName>.py
0

You need to specify the environment of the application. Like this

export FLASK_APP = application.py

after performing this operation

flask run

But my suggestion is that it will be easier for you to perform these operations there while creating your application, rather than constantly stating this in the terminal. After reviewing the documentation, if you do what I said, it will be enough to come to the terminal and run python app.py runserver in terminal.

You can check flask's documentation for that. https://flask.palletsprojects.com/en/1.1.x/patterns/appfactories/

0

I just had the same problem on Windows, using the command line. And problem was that I wasn't setting my app as the one that flask needs to run. So, in my case, I turned the debug mode on first (not required but convenient) with:

set FLASK_ENV=development

then:

set FLASK_ENV=server.py #where server.py is the name of my "app"

and finally ran flask:

flask run

0

Remove space between FLASK_APP and flaskblog.py, then execute flask run command or just run the program like other program python flaskblog.py

0

I had the same issue. These steps helped me:

  1. Delete existing venv environment, then in terminal type in project folder path py -3 venv venv
  2. Then: venv\Scripts\activate
  3. pip install flask
  4. set FLASK_APP=hello
  5. flask run

enter image description here

0

I was facing the same issue when doing flask run in the VScode.

But when I tried on CMD it worked fine

1
  • 1
    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.
    – Community Bot
    Dec 13, 2021 at 10:48
0

DO one thing

    from flask import Flask


app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'


app.run(debug=True)

python .\app.py

0

As Windows user, what helped me:

  1. set FLASK_APP=appname.py | in CMD
  2. $env:FLASK_APP="appname.py" | in PowerShell or VS Code(your code editing app)

And next line: flask run

0

for me adding this line at the end of the code fix it

if __name__=='__main__':
app.run()

so complete code would be this:

for more solution check this out common error with running flask in windows

1
0

In the command line, you'll run three lines of code. The first two lines tell the terminal where to find your application and to run it in development mode, which allows you to keep it running while it hotloads any modifications. The third actually starts the application.

# For Mac/Linux
 export FLASK_APP=app.py
 export FLASK_ENV=development
 flask run
# For Windows
  set FLASK_APP=app.py
  set FLASK_ENV=development
  flask run
-1

I had a same problem but i solve the issue by pasting the code in the python file.

if __name__=="__main__":
    app.run(debug=True)
1
  • 2
    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.
    – Community Bot
    Dec 23, 2021 at 9:34

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