inspect.getmodulename giving unexpected results

Geoff Bache geoff.bache at gmail.com
Fri Sep 24 11:49:56 CEST 2010


Hi all,

I'm trying to examine some things in my stack. The information I get
out of inspect.stack() gives file names and I would like to convert
them to module names. I naturally assumes inspect.getmodulename would
fix this for me.

Unfortunately, it doesn't seem to do that in some cases. Consider the
following code:

## file 'inspect_test'

#!/usr/bin/env python

import logging, inspect

print inspect.getmodulename(logging.__file__),
inspect.getmodulename(__file__)

I hoped that this would print
logging __main__

or at least
logging inspect_test

but instead it prints
__init__ None

which isn't very helpful, although it could technically be considered
correct (I can go to the 'logging' directory and type "import
__init__" but nobody ever does)

I guess I was hoping to get the names of the modules as they appear in
sys.modules. Maybe there is some other way to do that? I can naturally
write my own method to do this but wondered if I'm missing something
here.

Regards,
Geoff Bache



More information about the Python-list mailing list