Python Debugging with Traceback

I have problem when dealing with chaining function that throws error exception in each method calls like example below.

def func_a():
        # Error occured in here.
    except ValueError, e:
        raise e

def func_b():
    except ValueError, e:
        raise e

def func_c():
    except ValueError, e:
        raise e

if __name__ == '__main__':
    except Exception, e:
        raise e

From example above, I get used to use ipdb for debugging. Usuallly I find out one by one from func_c() until func_a() to locate the error.

I just found most simple way to deal with it without any extensions. We can use built in function traceback. Traceback will show in which line the error came from without interrupting your program.

Here is example how to use it:

import sys
import traceback

if __name__ == '__main__':
    except Exception, e:
        raise e

And the error stack trace example.

Traceback (most recent call last):
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/stocktracks/", line 155, in create_stpicklist_post_handler
    instance = STPicklist.objects.create(**picklist_meta)
  File "/home/adiyatmubarak/.virtualenvs/vasham/lib/python2.7/site-packages/django/db/models/", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/adiyatmubarak/.virtualenvs/vasham/lib/python2.7/site-packages/django/db/models/", line 372, in create, using=self.db)
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/stocktracks/", line 210, in save
    super(STPicklist, self).save(*args, **kwargs)
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/utils/", line 247, in save
    super(ModelDiffMixin, self).save(*args, **kwargs)
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/utils/", line 207, in save
    super(StripModelMixin, self).save(*args, **kwargs)
  File "/home/adiyatmubarak/.virtualenvs/vasham/lib/python2.7/site-packages/django/db/models/", line 589, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/adiyatmubarak/.virtualenvs/vasham/lib/python2.7/site-packages/django/db/models/", line 626, in save_base
    update_fields=update_fields, raw=raw, using=using)
  File "/home/adiyatmubarak/.virtualenvs/vasham/lib/python2.7/site-packages/django/dispatch/", line 198, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/stocktracks/", line 119, in signal_create_stactivity
    raise e
InvalidOperation: 0 / 0

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s