Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 629
Matt Avatar asked Nov 23 '14 09:11

Matt


People also ask

How do I turn on debug mode on PyCharm Flask?

An environment variable set to one of possible environments. The default value is 'development'. Select this checkbox to enable the built-in Flask debug mode. With this mode, the development server will be automatically reloaded on any code change enabling continuous debugging.

How do I enable debugging actions in PyCharm?

Just right-click any line in the editor and select the Debug <filename> command from the context menu. After the program has been suspended, use the debugger to get the information about the state of the program and how it changes during running.

Does PyCharm support Flask?

PyCharm supports Flask development including: Dedicated project type. Support for the built-in Flask debugger. Flask command-line interface (CLI).


1 Answers

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!

like image 83
Miguel Avatar answered Sep 21 '22 17:09

Miguel