How to debug a Flask app

前端 未结 13 2326
陌清茗
陌清茗 2020-11-22 03:08

How are you meant to debug errors in Flask? Print to the console? Flash messages to the page? Or is there a more powerful option available to figure out what\'s happening

相关标签:
13条回答
  • 2020-11-22 03:43

    Running the app in development mode will show an interactive traceback and console in the browser when there is an error. To run in development mode, set the FLASK_ENV=development environment variable then use the flask run command (remember to point FLASK_APP to your app as well).

    For Linux, Mac, Linux Subsystem for Windows, Git Bash on Windows, etc.:

    export FLASK_APP=myapp
    export FLASK_ENV=development
    flask run
    

    For Windows CMD, use set instead of export:

    set FLASK_ENV=development
    

    For PowerShell, use $env:

    $env:FLASK_ENV = "development"
    

    Prior to Flask 1.0, this was controlled by the FLASK_DEBUG=1 environment variable instead.

    If you're using the app.run() method instead of the flask run command, pass debug=True to enable debug mode.

    Tracebacks are also printed to the terminal running the server, regardless of development mode.

    If you're using PyCharm, VS Code, etc., you can take advantage of its debugger to step through the code with breakpoints. The run configuration can point to a script calling app.run(debug=True, use_reloader=False), or point it at the venv/bin/flask script and use it as you would from the command line. You can leave the reloader disabled, but a reload will kill the debugging context and you will have to catch a breakpoint again.

    You can also use pdb, pudb, or another terminal debugger by calling set_trace in the view where you want to start debugging.


    Be sure not to use too-broad except blocks. Surrounding all your code with a catch-all try... except... will silence the error you want to debug. It's unnecessary in general, since Flask will already handle exceptions by showing the debugger or a 500 error and printing the traceback to the console.

    0 讨论(0)
  • 2020-11-22 03:45

    with virtual env activate

    export FLASK_DEBUG=true
    

    you can configure

    export FLASK_APP=app.py  # run.py
    export FLASK_ENV = "development"
    

    to start

    flask run
    

    the result

     * Environment: development
     * Debug mode: on
     * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
     * Restarting with stat
     * Debugger is active!
     * Debugger PIN: xxx-xxx-xxx
    

    and if you change

    export FLASK_DEBUG=false
    
     * Environment: development
     * Debug mode: off
     * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
    
    0 讨论(0)
  • 2020-11-22 03:51

    You can use app.run(debug=True) for the Werkzeug Debugger edit as mentioned below, and I should have known.

    0 讨论(0)
  • 2020-11-22 03:54

    To activate debug mode in flask you simply type set FLASK_DEBUG=1 on your CMD for windows and export FLASK_DEBUG=1 on Linux termial then restart your app and you are good to go!!

    0 讨论(0)
  • 2020-11-22 03:56

    Use loggers and print statements in the Development Environment, you can go for sentry in case of production environments.

    0 讨论(0)
  • 2020-11-22 03:57

    Install python-dotenv in your virtual environment.

    Create a .flaskenv in your project root. By project root, I mean the folder which has your app.py file

    Inside this file write the following:

    FLASK_APP=myapp 
    FLASK_ENV=development
    

    Now issue the following command:

    flask run
    
    0 讨论(0)
提交回复
热议问题