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
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
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
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/
$> set FLASK_APP=application.py
$> flask run
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
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 environment
Thereafter, enter the following command ($set FLASK_APP=<your app name>.py
)
Enter the following command ($flask run
)