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

前端 未结 14 1904
囚心锁ツ
囚心锁ツ 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 10:50

    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)

    0 讨论(0)
  • 2021-02-04 10:50

    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?

    0 讨论(0)
  • 2021-02-04 10:52

    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!!!

    0 讨论(0)
  • 2021-02-04 10:57

    Try using app.py instead of different name

    0 讨论(0)
  • 2021-02-04 10:59

    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.

    0 讨论(0)
  • 2021-02-04 11:00

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