DaemonMaker
DaemonMaker

Reputation: 225

Automatically invoking pdb/ipdb while using shebang?

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

Upvotes: 2

Views: 485

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121914

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.

Upvotes: 1

Related Questions