PythonPython

Debugging Python Code

Nov 09, 2013 · Updated: Nov 04, 2017 · by Tim Kamanin

Debugging python code is extremelly simple and powerfull. Just use ipdb for
this purposes:

python
pip install ipdb

Then open up your code and in the place where you need code to stop insert
this:

python
import pdb; pdb.set_trace()

When you refresh the browser or execute a .py file, code execution will stop
in this place and controll will be passed to a command line. There you can do
whatever you want, like inspect local variables with

python
locals()

inspect global variables with

python
globals()

or execute any other functions. To continue just type

python
c

That's a real power! Compare it with PHP's pre, print or var_dump approaches.

Hey, if you've found this useful, please share the post to help other folks find it:

There's even more:

Subscribe for updates

  • via Twitter: @timonweb
  • old school RSS:
  • or evergreen email ↓ ↓ ↓