Alex Rivera | Logout

Find module name of the originating exception in Python

Asked 2009-07-08T00:55:16.850
10

Example:

>>> try:
...    myapp.foo.doSomething()
... except Exception, e:
...    print 'Thrown from:', modname(e)

Thrown from: myapp.util.url

In the above example, the exception was actually thrown at myapp/util/url.py module. Is there a way to get the __name__ of that module?

My intention is to use this in logging.getLogger function.

Edit
Report

1 Answer

0

This should do the trick:

import inspect
def modname():
    t=inspect.trace()
    if t:
        return t[-1][1]
answered 2009-07-08T01:06:58.827

Your Answer