In bottle all i have to do to enable debug mode is:
from bottle import debug
application = Bottle()
app = application
debug(True)
How can i do the same in Flask framework?
To enable the debug mode I would add the following code to the flask app:
app.config['ENV'] = 'development'
app.config['DEBUG'] = True
app.config['TESTING'] = True
I would also sugest setting environment variables for the environment and debug.
$ export FLASK_ENV=development
$ export FLASK_DEBUG=1
If you're in development environment; you can simply specify it in your Flask.run
as specified here .
Example:
app.run(host='0.0.0.0', port='8080', debug=True)
You can also take another approach at modifying the default_config
as shown here .
Example:
app.config['DEBUG'] = True
app.run(host='0.0.0.0', port='8080')
or even exporting FLASK_DEBUG=1
- Keep in mind that FLASK_DEBUG
overrides FLASK_ENV=development
.
As you can see below Flask denotes the following:
Do not enable debug mode when deploying in production.
Although as you stated you'd love to have debugging enabled on production; hence why I wouldn't use the Flask.run
since it's meant to be used on a development environment, and I would certainly not set the FLASK_ENV
to development
.
I would suggest you rethink the concept of debugging on production, since production is not meant for debugging; simply put, I believe your reasoning of wanting to debug on production is wrong.
Another possible solution is to use the following (forked, multi-process):
from werkzeug.debug import DebuggedApplication
application = DebuggedApplication(app, True)
With version 1.0 of Flask, you can set environment variables to be auto imported when you run the flask command, you need to have installed python-dotenv
though.
Then you can just create a new file in your top level directory named .flaskenv
and set your environment variables in it:
FLASK_APP=run.py
FLASK_DEBUG=1
Follow up with Gil Sousa'S answer, if anybody is facing the same issue in windows
add these three lines in you code in you file,
app.config['ENV'] = 'development'
app.config['DEBUG'] = True
app.config['TESTING'] = True
And also in the cmd
or in your anaconda prompt
type these commands to set up the debugging mode,
$ set FLASK_ENV=development
$ set FLASK_DEBUG=1
and don't forget to pass debug=True
inside app.run()
as a argument.
PS: if you are following this answer, FYKI, now flask apps are using the command $set FLASK_APP=app.py
then $FLASK run
to run a flask app. This is only for windows for Linux use export
instead of set
.
In the flask library, you can enable the built-in debug mode using the following steps.
Step 1: Create a virtual environment (a best practice) in your project file:
python -m venv webserver
Or, using python3
python3 -m venv webserver
where the webserver denotes the location of the virtual environment and the folder will be saved as the "webserver".
A best practice is to use the folder name as "venv".
Step 2: Using Windows Powershell, navigate to your project folder
cd "location of the project folder"
Step 3: Set the flask app variable to specify the python script name:
$env:FLASK_APP="server.py"
Step 4: Set the flask environment variable to specify the environment type as "development" to activate the debugger:
$env:FLASK_ENV="development"
Step 5: Activate the project's virtual environment:
venv\scripts\activate
Step 6: Run the flask application:
flask run
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.