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.

1
8
4/30/2015 7:01:45 AM

Accepted Answer

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...

2
10/6/2017 2:30:15 PM

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 -?

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon