<story><title>Debugging Python Like a Boss</title><url>https://zapier.com/engineering/debugging-python-boss</url></story><parent_chain><item><author>hcarvalhoalves</author><text>A good list of libraries, but please, don&amp;#x27;t use this in the middle of your code to set a break point:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    import pdb; pdb.set_trace();
&lt;/code&gt;&lt;/pre&gt;
There&amp;#x27;s a chance you forget this, check-in, and it ends in production. Use pdb facilities instead:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    $ python -m pdb &amp;lt;myscript&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
Then set a breakpoint and continue:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    (Pdb) break &amp;lt;filename.py&amp;gt;:&amp;lt;line&amp;gt;
    (Pdb) c
&lt;/code&gt;&lt;/pre&gt;
This is trivial to automate from any editor or command line, so you don&amp;#x27;t even have to guess the path to the file.&lt;p&gt;EDIT: For the lazy, here&amp;#x27;s a script to set breakpoints from the command line and run your scripts:&lt;p&gt;&lt;a href=&quot;https://gist.github.com/hcarvalhoalves/7587621&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;gist.github.com&amp;#x2F;hcarvalhoalves&amp;#x2F;7587621&lt;/a&gt;</text></item></parent_chain><comment><author>lewaldman</author><text>Here is my setup:&lt;p&gt;- I have a ~&amp;#x2F;.python&amp;#x2F;sitecustomize.py file with the following:&lt;p&gt;# Start debug on Exception&lt;p&gt;import bdb
import sys&lt;p&gt;def info(type, value, tb):&lt;p&gt;&lt;pre&gt;&lt;code&gt;   if hasattr(sys, &amp;#x27;ps1&amp;#x27;) \
         or not sys.stdin.isatty() \
         or not sys.stdout.isatty() \
         or not sys.stderr.isatty() \
         or issubclass(type, bdb.BdbQuit) \
         or issubclass(type, SyntaxError):
      # we are in interactive mode or we don&amp;#x27;t have a tty-like
      # device, so we call the default hook
      sys.__excepthook__(type, value, tb)
   else:
      import traceback, ipdb
      # we are NOT in interactive mode, print the exception...
      traceback.print_exception(type, value, tb)
      print
      # ...then start the debugger in post-mortem mode.
      ipdb.pm()
&lt;/code&gt;&lt;/pre&gt;
sys.excepthook = info&lt;p&gt;It will start ipdb automatically in case of any exception on command line called scripts.&lt;p&gt;- I use the awesome pdb emacs package for debug interactivelly during bigger bug hunts (Also for dev too... It&amp;#x27;s very a nice tool)&lt;p&gt;- Buutt... I still find the &amp;quot;print dance&amp;quot; to be my first-to-use quick tool.&lt;p&gt;edit: Fixed pasted code</text></comment>
<story><title>Debugging Python Like a Boss</title><url>https://zapier.com/engineering/debugging-python-boss</url></story><parent_chain><item><author>hcarvalhoalves</author><text>A good list of libraries, but please, don&amp;#x27;t use this in the middle of your code to set a break point:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    import pdb; pdb.set_trace();
&lt;/code&gt;&lt;/pre&gt;
There&amp;#x27;s a chance you forget this, check-in, and it ends in production. Use pdb facilities instead:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    $ python -m pdb &amp;lt;myscript&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
Then set a breakpoint and continue:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    (Pdb) break &amp;lt;filename.py&amp;gt;:&amp;lt;line&amp;gt;
    (Pdb) c
&lt;/code&gt;&lt;/pre&gt;
This is trivial to automate from any editor or command line, so you don&amp;#x27;t even have to guess the path to the file.&lt;p&gt;EDIT: For the lazy, here&amp;#x27;s a script to set breakpoints from the command line and run your scripts:&lt;p&gt;&lt;a href=&quot;https://gist.github.com/hcarvalhoalves/7587621&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;gist.github.com&amp;#x2F;hcarvalhoalves&amp;#x2F;7587621&lt;/a&gt;</text></item></parent_chain><comment><author>cjg_</author><text>If miss a line like that which is very easy to spot in a diff, which other things end up in your production environement?&lt;p&gt;And obviously any test covering that line will fail&amp;#x2F;hang.</text></comment>