18

I've just started using the nice PyCharm community edition IDE, and can't do a simple thing that is part of my usual Python workflow.

I've started an ipython console and I can import my modules and interactively run commands. In PyCharm when I execute a function call, it is executed like it was running in a separate process. The console prompt can be used even before the execution finishes. It is nice but prevents me to do post mortem debugging.

Running ipython in a shell outside PyCharm, when an exception happens, I can run pdb's post mortem feature and investigate the problem:

import pdb;pdb.pm()

I want to do the same in PyCharm: start post mortem debugging when an exception happens while I'm interactively investigating a problem.

neves
  • 33,186
  • 27
  • 159
  • 192

3 Answers3

4

Given that you are using PyCharm, why not use the built-in debugger? There is ample documentation on how you can use it to set your own breakpoints, step through your stack, configure the debugger options etc.

Specifically for your use case, if you want to break on exceptions -- you can use Ctrl + Shift + F8 to open the breakpoints configuration screen. Breakpoints You can then check to break on Any exception for Python Exception Breakpoint. This would give you access to the exception and the entire stack (in the Frames section), context, variables etc. when something does happen.

Vlad B
  • 323
  • 3
  • 11
  • 3
    Because some times you have to interactively experiment with your code, ideas and data. During these interactive sessions, I want to be able to do post mortem debugging. It is a different use case. – neves Nov 04 '16 at 15:20
  • I see -- must have misunderstood your question. Hope you manage to find a way to do what you are after. If you are working on something using WSGI would Werkzeug (http://werkzeug.pocoo.org/docs/0.11/ ) work for you? – Vlad B Nov 04 '16 at 15:25
  • There is an interactive ipython console you can get to inside the PyCharm debugger, but it's a little hidden -- you have to click "open debug command line". Combine this with the frames / variables view in pycharm debugger and you have roughly the same functionality as pdb.pm. https://www.jetbrains.com/help/pycharm/using-debug-console.html I recommend editing this answer to add debug console info, then I think it should be marked correct. – foobarbecue Oct 21 '17 at 18:16
  • Mm, on second thought, that answer still wouldn't be correct enough. I can't find a way to go into debugger after entering a command in interactive session. That's really a drag. – foobarbecue Oct 21 '17 at 18:21
  • 1
    BTW, there are TWO buttons you have to click to make this work. First, switch to debugger console tab, and then click "show python prompt" to actually make the console interactive. – foobarbecue Oct 21 '17 at 18:25
  • I don't get how it works. Do you have more information about it? – Smit Johnth Dec 07 '21 at 07:05
2

It is possible in IPython to use the %pdb magic.

Simply use %pdb on IPython and whenever an error occurs you will be dropped in an ipdb session with the Post Mortem of the error.

I don't really know if this works in PyCharm.

(I found this answer here some time ago)

renan-eccel
  • 181
  • 10
0

Due to the lack of answers, I think it isn't possible to do. If someone posts an answer here I'll accept it.

BTW, if you have Jupyter installed, it will give you a marvelous terminal if you run jupyter qtconsole. Works very well even in Windows. The magic command %debug will automagically start the debugger.

neves
  • 33,186
  • 27
  • 159
  • 192