18
votes

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

15
You 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 - Solaiman

15 Answers

18
votes

Windows PowerShell

set FLASK_APP=hello.py
$env:FLASK_APP = "hello.py"
flask run
15
votes

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
6
votes

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.

5
votes

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)

4
votes

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?

1
votes

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
votes

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
votes

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
1
votes

Try using app.py instead of different name

1
votes
$> set FLASK_APP=application.py
$> flask run
0
votes

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)

0
votes

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
0
votes

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

0
votes

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
votes

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