FI-Info
FI-Info

Reputation: 705

How to get the module name in an exception

In trapping an exception I have gotten the following Exception:

>>> exc_type
<class 'pymysql.err.ProgrammingError'>

Is there a simple way to get the value of pymysql so I can see which import/module it's coming from? Current I'm doing:

>>> exc_type.__module__.split('.')[0]
'pymysql'

But this seems a bit crude. What would be the best way to get that value?

Upvotes: 2

Views: 291

Answers (1)

sanyassh
sanyassh

Reputation: 8500

You can try inspect.getmodule:

>>> module = inspect.getmodule(exc_type)
>>> module
<module 'pymysql.err' from 'C:\\Python36\\lib\\site-packages\\pymysql\\err.py'>
>>> module.__name__
pymysql.err
>>> module.__package__
pymysql

Upvotes: 1

Related Questions