Determine calling module's name

jeffgray at my-deja.com jeffgray at my-deja.com
Thu Aug 10 02:11:02 EDT 2000


How do I determine the calling module's name? i.e. from within a
function, how can I print the __name__ of the next level up in the stack
frame?

I had hoped this would work:

def DisplayMessage(message, modname=__name__):
    print module,':',message

It does work if I explicitly define the modname parameter, but if I
don't specify the modname parameter, it sets modname to the module that
this function resides in. I can understand why this might happen, but
it's very frustrating.

My next thought was that there must be a way to determine the calling
module name. I looked at the standard traceback module and all the
sys.exc_info() type functions, but they only work with an exception.

So what are my alternatives? If I have to explicitly fill in the
module name with every call, so be it, but I felt there had to be a
smarter way.


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list