Enable debug mode in Flask in production mode

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?

Upvotes: 8

Views: 47660

Answers (7)

ahmnouira
ahmnouira

Reputation: 3451

Just do:

export FLASK_DEBUG=1
# then 
flask run

With one line

FLASK_DEBUG=1 flask run

Upvotes: 0

Gil Sousa
Gil Sousa

Reputation: 844

To enable the debug mode I would add the following code to the flask app:

app.config['ENV'] = 'development'
app.config['DEBUG'] = True

I would also sugest setting environment variables for the environment and debug.

$ export FLASK_ENV=development 
$ export FLASK_DEBUG=1

If you are jus trying flask locally and don't want use one of the two methods above you can also run your flask app in debug mode with the following line at the command line:

flask run --debug

Upvotes: 18

hansrajswapnil
hansrajswapnil

Reputation: 639

I am developing a webapp using flask 2.0 in vs code.

I faced problems running my flask application. Here's what helped: -
To run your flask application -

 C:\Users\hansr\OneDrive\Desktop\python_dev\postgresdemo> $env:FLASK_APP='main'

To set the Debug mode ON -

C:\Users\hansr\OneDrive\Desktop\python_dev\postgresdemo> $env:FLASK_DEBUG=1

Now, run your flask app -

C:\Users\hansr\OneDrive\Desktop\python_dev\postgresdemo> python -m flask run

Reference

Upvotes: 0

Aswin Barath
Aswin Barath

Reputation: 1107

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

Upvotes: 1

Soumyadip Sarkar
Soumyadip Sarkar

Reputation: 321

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.

Upvotes: 4

Daniel Romero
Daniel Romero

Reputation: 1598

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

Upvotes: 1

Julian Camilleri
Julian Camilleri

Reputation: 3125

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)

Upvotes: 4

Related Questions