using the pycharm debugger with a flask application factory

Question:

I am typing more print statements than code. It’s killing me.

If a flask development server is invoked via below, I can use PyCharm debugger

from ersapp import app

if __name__ == '__main__':
    app.run(debug=True)

I am following the example in Miguel Grinberg’s book and an application manager (flask-script) is used. I invoke the server in my application directory like below

(env)$ python manage.py runserver

and in appdirectory/__init__.py

def create_app(config_name):
    webapp = Flask(__name__)
    ...
    return webapp

The debugger in Pycharm would make things easier since that’s where I work.

Asked By: Brian Leach

||

Answers:

You ran the project manually by CLI. For using PyCharm IDE debug you must configure PyCharm for your project and then run this by PyCharm.
But if you want to run the program without PyCharm, you can use the pdb library for debugging destinations. Try the code below:

import pdb


def my_def():
    try:
        x = 7 / 0
    except Execption as e:
        pdb.set_trace()

When running this program you can see the interactive line on your CLI…

Answered By: M.javid

I experienced the same problem working through Miguel Grinberg’s book. To answer the question How to "Configure PyCharm" for your project I offer the following comment.

To remain in PyCharm to take advantage of its glorious debugger go to Edit Configurations, and in that dialog box make sure you are on the Configurations tab. There, the two top text boxes are:

Script: set to path of your manage.py

Script parameters: runserver

By the way I am using PyCharm 4.5.3, although I suspect the following is true in at least a few of the previous releases I have worked in. Now running the application from PyCharm invokes the runserver command:

python manage.py runserver 

and this runs the flask development server, i.e. app.run(). The Configuration tab has allowed us to specify running the particular script manage.py, as well as the command line argument to use, e.g. runserver as in this case. After running the app in PyCharm look at the top line in the output in the Run or Debug window and you will see among other entries: –file pathto/manage.py runserver.

You might have specified shell instead of runserver in the script parameter text box, and in that case you would have found yourself in the shell after running the app in PyCharm.

The default Manager(app) commands are runserver and shell. The db command is added in the following line of manage.py:

manager.add_command('db', MigrateCommand) 

Underneath that the command test is added. Notice the @manager.command decorator prior to def test().

To get a list of all Manager(app) commands type on the command line:

python manage.py

If you are at the Application Factory part of the tutorial you should see {test, shell, db, runserver}. To get help on any one command type:

python manage.py parameter -?
Answered By: user3842449

Try configuring this python running configuration in “Edit Configurations”. After that, run in debug mode.

PyCharm configuration example

Answered By: Ráfagan

If you are using the application factory pattern (i.e. using creat_app() WITHOUT a run.app() main) you can use your standard ‘flask’ run configuration template (community version may not have these, not sure). However, you’ll notice that the debugger wont stop at breakpoints because the flask app in DEBUG runs the reloader which means it runs in different threads and Pycharm cant catch it. So to make it break not just at lunch but any API call you want to debug make sure you:

  • select DEBUG checkbox
  • add –no-reload as a flask argument
  • add –without-threads as a flask argument

This was the only way I could get full debug support:

Run Configuration

Answered By: jpmorris

Finally I’ve handled this issue with pyCharm pro 2022, the issue in my opinion was in IDE Flask template, so I might to use regular Python project with settins as follows:

1). Set env:

FLASK_DEBUG = 1

2). Use pyCharm’s regular Python project:
pyCharm settings to make Flask debug mode active

Answered By: Vitalii Mytenko