Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flask run does not respect debug config in config files #1979

Closed
abendebury opened this issue Aug 8, 2016 · 8 comments
Closed

flask run does not respect debug config in config files #1979

abendebury opened this issue Aug 8, 2016 · 8 comments

Comments

@abendebury
Copy link

To reproduce:

https://github.com/PlasmaSheep/flask-bug

Clone repo and install flask (python2)

export FLASK_APP=/path/to/repo/flask-bug/autoapp.py 
flask run

Notice the output:

[$]>>> flask run
Using config: development
Debug is: True
 * Serving Flask app "autoapp"
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

So debug is enabled in the config, but the debugger is not actually active.

This does work if you set the FLASK_DEBUG env variable:

[$]>>>  flask run
 * Serving Flask app "autoapp"
 * Forcing debug mode on
Using config: development
Debug is: True
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
Using config: development
Debug is: True
 * Debugger is active!
 * Debugger pin code: 155-859-497

However I think that the debug parameter should be specified in only one place, and that place should be the application config file so that you don't have to worry about setting environment variables for things you already specified in the config.

@abendebury
Copy link
Author

And furthermore, FLASK_DEBUG does not work when you are using a custom manage.py script:

>> ./manage.py run
 * Forcing debug mode on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 155-859-497
127.0.0.1 - - [12/Aug/2016 17:01:07] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/plasma/.virtualenvs/quizApp/lib/python2.7/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/plasma/.virtualenvs/quizApp/lib/python2.7/site-packages/click/globals.py", line 26, in get_current_context
    raise RuntimeError('There is no active click context.')
RuntimeError: There is no active click context.
─[$]>>> cat manage.py 
#!/usr/bin/env python
"""Management script for common operations.
"""
import subprocess
import sys

from flask import current_app
from flask.cli import FlaskGroup
import click

from quizApp import create_app

@click.pass_context
def get_app(ctx, _):
    """Create an app with the correct config.
    """
    return create_app(ctx.find_root().params["config"])


@click.option("-c", "--config", default="development",
              help="Name of config to use for the app")
@click.group(cls=FlaskGroup, create_app=get_app)
def cli(**_):
    """Define the top level group.
    """
    pass

if __name__ == '__main__':
    cli()

@liuliqiang
Copy link

liuliqiang commented Aug 24, 2016

@PlasmaSheep thks for your good question...

however maybe there has something mistake with your understand about debug mode.

  1. FLASK_DEBUG enviroment arg only work on flask command .

The flask script can also be instructed to enable the debug mode of the application automatically by exporting FLASK_DEBUG. If set to 1 debug is enabled or 0 disables it.

maybe it's pocoo's misatake for not describe it clearly.

  1. using flask command, it call the Flask object run function in lower level implements, so if your development config not work for command line execute. And the same, only command line would read the FLASK_DEBUG env arg, and if you using manage.py to run your app, it also not affect the debug mode.

in flask-cli.py Line 402 you would see that flask command run simple_run command.

@untitaker
Copy link
Contributor

@luke0922 is right about FLASK_DEBUG.

Fair enough about the second example. I'm not sure about whether there exists a good solution though.

@abendebury
Copy link
Author

@luke0922:

FLASK_DEBUG enviroment arg only work on flask command .

In this case I am not surprised, I wish the documentation was a bit clearer though.

using flask command, it call the Flask object run function in lower level implements, so if your development config not work for command line execute. And the same, only command line would read the FLASK_DEBUG env arg, and if you using manage.py to run your app, it also not affect the debug mode.

Yes, but the issue is that there seems to be no way to enter debug mode when using a custom management script. The only way to use debug mode is to have an additional file which creates an application from the factory.

@untitaker
Copy link
Contributor

The only way to use debug mode is to have an additional file which creates an application from the factory.

I don't understand where you're getting that. You can set app.debug=True on the app, you don't need to call create_app again.

@davidism
Copy link
Member

possible duplicate of #1641

@davidism
Copy link
Member

app.debug just turns on debug behavior for the application, while FLASK_DEBUG turns on debugging (and reloading) behavior for the server as well as setting app.debug = True.

It's hard to go the other direction because the app may be lazy loaded, so you can't determine its debug state until it's actually loaded, which doesn't happen until after the server is started.

@abendebury
Copy link
Author

I don't understand where you're getting that. You can set app.debug=True on the app, you don't need to call create_app again.

This doesn't have the same functionality as FLASK_DEBUG , since it seems that reloading is only enabled on FLASK_DEBUG.

possible duplicate of #1641

Actually yes, this did not turn up in my searches. I'll close this, sorry about that.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants