'How to setup environment variables for `flask run` on Windows?
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" ?
Solution 1:[1]
Windows PowerShell
set FLASK_APP=hello.py
$env:FLASK_APP = "hello.py"
flask run
Solution 2:[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:[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:[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:[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
Solution 6:[6]
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?
Solution 7:[7]
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!!!
Solution 8:[8]
You can try this
set FLASK_APP=application.py
flask run
Solution 9:[9]
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.
Solution 10:[10]
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.
Solution 11:[11]
A step-wise solution is provided below:
Go to the folder where you have placed your flask app (on the command line)
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.Enter the following command (
$env\Scripts\activate
) by pressing enter this will turn on your virtual environmentThereafter, enter the following command (
$set FLASK_APP=<your app name>.py
)Enter the following command (
$flask run
)
Solution 12:[12]
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
Solution 13:[13]
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
Solution 14:[14]
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!
Solution 15:[15]
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
Solution 16:[16]
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
Solution 17:[17]
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/
Solution 18:[18]
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
Solution 19:[19]
Remove space between FLASK_APP and flaskblog.py, then execute flask run command or just run the program like other program python flaskblog.py
Solution 20:[20]
I had the same issue. These steps helped me:
- Delete existing
venv
environment, then in terminal type in project folder pathpy -3 venv venv
- Then:
venv\Scripts\activate
pip install flask
set FLASK_APP=hello
flask run
Solution 21:[21]
I was facing the same issue when doing flask run in the VScode.
But when I tried on CMD it worked fine
Solution 22:[22]
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
Solution 23:[23]
As Windows user, what helped me:
- set FLASK_APP=appname.py | in CMD
- $env:FLASK_APP="appname.py" | in PowerShell or VS Code(your code editing app)
And next line: flask run
Solution 24:[24]
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
Solution 25:[25]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow