2

Is there a way to reliably get an object's package name? I'm trying to programmatically determine if a connector is from mysqldb, pyscopg2, cx_oracle, or pymssql. I had been using the conn.__class__ and regular expression previously but then stumbled when MySQLdb broke the pattern.

psycopg2: <type 'psycopg2.extensions.connection'>

oracle: <type 'cx_Oracle.Connection'>

pymssql: <type 'pymssql.Connection'>

mysqldb: MySQLdb.connections.Connection

I'm wondering if there's a builtin python method of finding a classes' package. Ideally, this would simply return psycopg2, cx_Oracle, pymssql, or MySQLdb

Yes, thank you, Get fully qualified class name of an object in Python was the first thing that came up when I searched also. psycopg2, cx_oracle, and pymssql all return AttributeError: 'Connection' object has no attribute '__module___'

Sebastian
  • 1,623
  • 19
  • 23

1 Answers1

0

Use inspect.getmodule().

You may inspect the module's __package__ after you get the module if it doesn't suffice.

Another option would be to get use inspect.getfile() and search for the known strings.

Bharel
  • 23,672
  • 5
  • 40
  • 80