Matt
Matt

Reputation: 1343

pycharm and flask autoreload and breakpoints not working

I'm using Pycharm 4, with flask 0.10.1, python 3.4

It seems that when running a flask application from inside pycharm, if I run it with:

app.run(debug=True)

My breakpoints are ignored. After some googling, I've found that in order to make PyCharm stop on breakpoints, I should run flask with:

app.run(debug=True, use_reloader=False)

Now PyCharm correctly stops on breakpoints, but I miss the autoreloading feature.

Is there any way to make both work together?

Using python 2.7 both things work

I reported this to PyCharm: https://youtrack.jetbrains.com/issue/PY-13976

Upvotes: 30

Views: 17186

Answers (7)

Koroslak
Koroslak

Reputation: 736

You need to unlock the console.

  1. you start the app in debug mode

enter image description here

  1. then you make something that causes an error.
  2. at the end of the error message from flask is this

enter image description here

  1. Go to localhost:5000/console and enter the PIN flask prints in the console at the start enter image description here
  2. copy paste this pin into the console and click confirm PIN
  3. now the breakpoints will work

Upvotes: 2

Vito
Vito

Reputation: 1698

In my setup, I'm debugging the flask app by running a main.py file which sets some configuration and calls app.run(). My python interpreter is set up in a Docker container.

My issue was that I needed to check Run with Python console.

Upvotes: 2

Ráfagan
Ráfagan

Reputation: 2255

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

Example

Upvotes: 0

klaxon
klaxon

Reputation: 415

I found that in PyCharm 2018.1.2 there is FLASK_DEBUG checbox in run configuration:enter image description here

With this after making some changes, saving file triggers reload action.

Upvotes: 7

mattg
mattg

Reputation: 1

from pycharm 2017 using python 2.7 (in my case with virtual env, but I suppose not necessary) I do:

  • run...
  • leave scripts and scripts parameters blank
  • I put in interpreter options: -m flask run
  • set the env variables FLASK_APP

  • than run attach to local process, and finally choose the running process

my use case is to connect from postman to flask rest services endpoints and interrupt on my breakpoints

Upvotes: -1

Miguel Grinberg
Miguel Grinberg

Reputation: 67502

I'm going to start with the short answer: No, what you want cannot be done with any releases of PyCharm up to 4.0.1.

The problem is that when you use the reloader the Flask application runs in a child process, so the PyCharm debugger is attached to the master process and has no control over the child.

The best way to solve this problem, in my opinion, is to ask Jetbrains to build a "restart on change" feature in their IDE. Then you don't need to use Werkzeug's reloader at all and you get the same functionality direct from PyCharm.

Until Jetbrains decides to implement this, I can share my workaround, which is not terribly bad.

  • In the "Edit Configurations", set the configuration you are going to use to "Single Instance only" (check box in the top right of the dialog box)
  • Make sure the configuration is the active one.
  • Configure your Flask app to not use the Werkzeug reloader.
  • Press Ctrl-D to start debugging (on Mac, others may have a different shortcut)
  • Breakpoints should work just fine because the reloader isn't active.
  • Make any code changes you need.
  • When you are ready to restart, hit Ctrl-D again. The first time you do it you will get a confirmation prompt, something like "stop and restart?". Say yes, and check the "do not show again" checkbox.
  • Now you can hit Ctrl-D to quickly restart the debugger whenever you need to.

I agree it is not perfect, but once the Ctrl-D gets into your muscle memory you will not even think about it.

Good luck!

Upvotes: 26

Andrew Dunai
Andrew Dunai

Reputation: 3129

The problem is because with use_reloader=True the werkzeug application is started in a seperate (child) thread of main application and PyCharm fails to correctly handle breakpoints because they are lost when the thread starts.

You can try to follow this thread: http://forum.jetbrains.com/thread/PyCharm-776 but it seams there was not too much progress on that.

I'd suggest using something Python-ish like pdb, i.e.:

@app.route('/<string:page>')
def main(page):
   import pdb; pdb.set_trace()  # This line actually stops application execution
                                # and starts Python debug shell in the console
                                # where you can examine current scope and continue
                                # normal code execution at any time.
                                # You can inject *any* code here.
                                # For example, if you type `print page` during pause,
                                # it will output content of "page" variable.
   return render_template('index.html')

Upvotes: 1

Related Questions