I’m trying to get the name of a calling module by using inspect. When I return what should be the module string, I get this:
<module 'mymod.wrapper' from '/usr/local/lib/python2
I’ve looked at the docs and couldn’t see anything about what the reason for this may be (I’m tired, so I may have missed it).
Here is the class
import inspect
class Wrapper():
def getView(self, view, database=False):
module = self._getDatabase()
print(module)
def _getDatabase(self):
# Get calling module
frm = inspect.stack()[1]
modWhole = str(inspect.getmodule(frm[0]))
modSplit = modWhole.split('.')
mod = modSplit[0] + '.' + modSplit[1]
return mod
Also, any advice on how to reconize and prevent this problem in the future is much appreciated. Thanks.
inspect.getmodule()returns the module object itself, not its name.Try replacing
with
Also, bear in mind that
inspect.getmodule()can returnNone.