How to run a flask application?

前端 未结 4 990
傲寒
傲寒 2020-11-27 04:52

I want to know the correct way to start a flask application. The docs show two different commands:

$ flask -a sampl         


        
相关标签:
4条回答
  • 2020-11-27 05:07

    For Linux/Unix/MacOS :-

    export FLASK_APP = sample.py
    flask run
    

    For Windows :-

    python sample.py
          OR
    set FLASK_APP = sample.py
    flask run
    
    0 讨论(0)
  • 2020-11-27 05:09

    Latest documentation has the following example assuming you want to run hello.py(using .py file extension is optional):

    Unix, Linux, macOS, etc.:

    $ export FLASK_APP=hello
    $ flask run
    

    Windows:

    > set FLASK_APP=hello
    > flask run
    
    0 讨论(0)
  • 2020-11-27 05:16

    The flask command is a CLI for interacting with Flask apps. The docs describe how to use CLI commands and add custom commands. The flask run command is the preferred way to start the development server.

    Use the FLASK_APP environment variable to point the command at your app. Set FLASK_ENV=development to run with the debugger and reloader. Never use this command to deploy publicly, use a production WSGI server such as Gunicorn, uWSGI, Waitress, or mod_wsgi.

    $ export FLASK_APP=sample
    $ export FLASK_ENV=development
    $ flask run
    

    On Windows CMD, use set instead of export.

    >set FLASK_APP=sample
    

    The python sample.py command runs a Python file and sets __name__ == "__main__". If the main block calls app.run(), it will run the development server.

    if __name__ == "__main__":
        app.run(debug=True)
    

    Both these commands ultimately start the Werkzeug development server, which as the name implies starts a simple HTTP server that should only be used during development. You should prefer using the flask run command over the app.run() method.

    0 讨论(0)
  • 2020-11-27 05:21

    it will work in cmd only if you type

    > pipenv shell 
    

    start subshell in virtual environment first then type

    > set FLASK_APP=hello
    > flask run
    
    0 讨论(0)
提交回复
热议问题