Skip to content Skip to sidebar Skip to footer

Can't Enable Debug Mode In Flask

I have a fairly basic Flask app, but for some reason Debug mode wont enable, so whenever I get an error I get a 500 page instead of the nice debug page with the traceback and all t

Solution 1:

The debugger is part of the WSGI runner; the app.run() server. If you use a different WSGI server you need to explicitly add the debugger middleware:

defcreate_app(config_name):
    app = Flask(__name__)

    # ...if app.debug:
        from werkzeug.debug import DebuggedApplication
        app.wsgi_app = DebuggedApplication(app.wsgi_app, True)

    return app

When you use Flask-Script, the runserver runs the Flask WSGI development server and will enable the debugger.

Unfortunately, Flask-Script version 2.0.3 introduced a bug; it doesn't set up the new debug flags correctly and always disabled the debugger unless you explicitly pass in the -d flag. This is regardless of wether you set use_debugger to true; this because the default of an argparsestore_true option is to store False instead when not picked.

The work-around is to explicitly use -d, or to patch flask_script/commands.py to give all --debug and --no-debug options self.use_debugger as the default:

ifself.use_debugger:
    options += (Option('-d', '--debug',
                       action='store_true',
                       dest='use_debugger',
                       help="(no-op for compatibility)",
                       default=self.use_debugger),)
    options += (Option('-D', '--no-debug',
                       action='store_false',
                       dest='use_debugger',
                       default=self.use_debugger),)

else:
    options += (Option('-d', '--debug',
                       action='store_true',
                       dest='use_debugger',
                       default=self.use_debugger),)
    options += (Option('-D', '--no-debug',
                       action='store_false',
                       dest='use_debugger',
                       help="(no-op for compatibility)",
                       default=self.use_debugger),)

where I've added default=self.use_debugger to the two options that did not yet have it set.

The handling of self.use_reloader is similarly flawed.

Versions 0.6.7 and 1.0 don't suffer from this bug; a fix has been committed for version 2.0.4 (not as yet released).

Post a Comment for "Can't Enable Debug Mode In Flask"