using the pycharm debugger with a flask application factory
Asked Answered
W

5

17

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.

Wellread answered 30/4, 2015 at 6:55 Comment(0)
S
1

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

Shortlived answered 30/4, 2015 at 7:2 Comment(1)
very helpful. Can you suggest more pycharm resources?Wellread
V
12

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

PyCharm configuration example

Venicevenin answered 15/9, 2018 at 1:21 Comment(2)
This is not portable in a team environmentPolka
youtube.com/watch?v=I5XRG9hl8RI&t=7s This shows few options that you need to configureVexatious
U
8

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

Updo answered 6/7, 2020 at 21:10 Comment(2)
PyCharm Community, version 2020.2.1. I added --no-reload, --no-debugger and the only thing missing was --no-threads. Once I added those 3 params, the app started stopping on breakpoints.Froude
Thanks, adding --no-debugger was what I needed to get the debugger working....Forenoon
S
1

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

Shortlived answered 30/4, 2015 at 7:2 Comment(1)
very helpful. Can you suggest more pycharm resources?Wellread
I
1

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

Inainability answered 20/8, 2022 at 15:10 Comment(0)
L
0

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 -?
Litharge answered 27/8, 2015 at 1:8 Comment(6)
Not sure why this is got down voted. It was built and tested with Pycharm, as well as the app both on the host machine. It was also built and tested in an environment where Pycharm was on the host machine with remote debugging enabled, and the app on a Linux virtual machine.Litharge
you are referencing 'manage.py' when this is a flask app not django.Updo
@Updo You are most certainly incorrect. Check out O'Reilly Flask Web Development under Large Application Structure:Launch Script. There is an example there of implementing manager.py for Flask (from flask.ext.script import Manager). Did you read my whole comment where I mentioned it was fully tested in Flask.Litharge
Yes, Miguel Grindberg's first edition Flask books use manage.py because using Flask-Script at that time needed a script file and Miguel named it manage.py. But that has long stopped being the case, so this answer is, at best, outdated.Diannadianne
@martijn-pieters Yes, the original question was posted in 2015-04. The answer was given contemporaneously in 2015-07. At the time it was up to date. 1st ed. published in 2014-05. 2nd ed. published in 2018-03. I hope it is obvious that it is outdated since it is 5 yrs old! You state the obvious. The answer wasn't downvoted in 2020 because it was out-of-date. It was downvoted because the commenter, who was referring to the original answer given in 2015, erroneously assumed I was referring to Django, when the answer clearly states I was using Flask manage.py.Litharge
@Aaron: you are conflating a comment with why someone voted. Stop doing that. And it's one point of view, a calm and measured response in a comment can easily counter it. In the long run, those single comments will not stop a helpful answer from gaining more upvotes.Diannadianne

© 2022 - 2024 — McMap. All rights reserved.