2

I want to use a shebang line in my scripts, e.g.

#! /usr/env/bin python

but when debugging I also want pdb/ipdb to be invoked automatically as in:

python -m ipdb myscript.py

Is there a way to combine these? In other words, is there a version of the shebang that will also automatically invoke pdb/ipdb upon a failure? Something like:

#! /usr/env/bin python -m ipdb
DaemonMaker
  • 225
  • 2
  • 12

1 Answers1

1

You cannot easily pass extra arguments on a shebang line, because the shell doesn't parse out the arguments. There are work-arounds for that.

I'd instead invoke the post-mortem debugger on exception, however. If you have a main() function in your script, I'd use:

try:
    main()
except Exception:
    import ipdb, sys
    ipdb.post_mortem(sys.exc_info()[2])

where ipdb.post_mortem() must take a traceback object. The pdb.post_mortem() version doesn't need it as it picks this up by itself if no traceback was passed in.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Evidently that does not stop the debugger on the offending line. – DaemonMaker Jan 01 '15 at 16:43
  • @DaemonMaker: It does exactly what the `pdb` and `ipdb` modules do; catch the exception and take the current traceback as the context for the post-mortem debug session. – Martijn Pieters Jan 01 '15 at 16:46
  • Based on a quick test it does not. Executing a simple test script with an intentional error and your prescribed fix results in: $ ./test.py testing ---------------------------------------- AttributeError Traceback (most recent call last) /home/dustin/test.py in () 10 except Exception: 11 import ipdb ---> 12 ipdb.pm() /usr/lib/python2.7/dist-packages/ipdb/__main__.pyc in pm() 106 107 def pm(): --> 108 post_mortem(sys.last_traceback) 109 110 AttributeError: 'module' object has no attribute 'last_traceback' – DaemonMaker Jan 01 '15 at 16:49
  • @DaemonMaker: I replaced `.pm()` with `.post_mortem()` as it uses a better method to pick up the exception. With `def main(): 0 / 0` then the above, the debugger steps into the debugger correctly. – Martijn Pieters Jan 01 '15 at 16:52
  • Okay, so please excuse the formatting of my last comment. That's pretty much useless. The point is tried your solution and it provides a post mortem but does not invoke the debugger for interaction. I just tried changing .pm() to .post_mortem() and it behaves the same. – DaemonMaker Jan 01 '15 at 16:54
  • @DaemonMaker: all I can say now is *it works for me*. I tested this with both `pdb` and `ipdb`. – Martijn Pieters Jan 01 '15 at 16:56
  • Well, thanks for trying @Martijn but if it doesn't work for me it doesn't constitute an acceptable answer. ;) – DaemonMaker Jan 01 '15 at 17:05
  • @DaemonMaker: I tested with [this source](https://gist.github.com/mjpieters/948b91109da46fced225) on Python 3.4 and 2.7. What version of ipdb are you using? – Martijn Pieters Jan 01 '15 at 17:17
  • I'm using Python 2.7.6. I don't see versioning information for ipdb anywhere. – DaemonMaker Jan 01 '15 at 18:20
  • Yes, if I use pdb as in the script you link then it works as desired. However that script is not using the technique that you posted in your answer. The difference being that your answer uses ipdb and the script uses pdb. – DaemonMaker Jan 01 '15 at 18:24
  • @DaemonMaker: clarified some more for `ipdb` specifically. I used 0.8, released in September 2013. You most likely have the same. – Martijn Pieters Jan 01 '15 at 18:24
  • I just noticed that you updated the answer again and that solution (with ipdb) works for me. – DaemonMaker Jan 01 '15 at 18:26