Find module name of the originating exception in Python
问题 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. 回答1: This should work: import inspect try: some_bad_code() except Exception, e: frm = inspect.trace()[-1] mod = inspect.getmodule(frm[0]) print 'Thrown from', mod._