ymmx
ymmx

Reputation: 4967

Is it possible to find out from which module a class instance come from?

I would like to know if it is possible to obtain the module name and directory path from a class instance of that module. I need that because I save a class instance with pickle and I need to know from where this class come from when I load it from another software (on the same computer).

update

So I tried the aswer of @albar

>>> from Models import Model_physio_moyen
>>> b = Model_physio_moyen.pop_Sigmoid_outside_noise
>>> module_name = sys.modules[b.__module__].__name__
>>> module_name
'Models.Model_physio_moyen'

So that working except if the class has been compiled with jitclassof numba:

>>> from Models.Pops_Stim import ATN
>>> a = ATN.pop_ATN()
>>> module_name = sys.modules[a.__module__].__name__
>>> module_name
'numba.jitclass.boxing'

In this condition, I wish I could find Models.Pops_Stim but I'm getting 'numba.jitclass.boxing'instead.

Upvotes: 1

Views: 44

Answers (1)

albar
albar

Reputation: 3100

Assuming your instance is called a:

import sys
module_name = sys.modules[a.__module__].__name__
module_file = sys.modules[a.__module__].__file__

Upvotes: 1

Related Questions