How to setup environment variables for `flask run` on Windows?

前端 未结 14 1871
囚心锁ツ
囚心锁ツ 2021-02-04 10:19

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

相关标签:
14条回答
  • 2021-02-04 11:02

    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
    
    0 讨论(0)
  • 2021-02-04 11:02

    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 讨论(0)
  • 2021-02-04 11:04

    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 讨论(0)
  • 2021-02-04 11:07
    $> set FLASK_APP=application.py
    $> flask run
    
    0 讨论(0)
  • 2021-02-04 11:09

    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 讨论(0)
  • 2021-02-04 11:12

    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 讨论(0)
提交回复
热议问题