Take the 2-minute tour ×

Suppose myapp/foo.py contains:

def info(msg):
    caller_name = ????
    print '[%s] %s' % (caller_name, msg)

And myapp/bar.py contains:

import foo
foo.info('Hello') # => [myapp.foo] Hello

I want caller_name to be set to the __name__ attribute of the calling functions' module (which is 'myapp.foo') in this case. How can this be done?

share|improve this question
Assume that some other entry point script invokes bar.py .. and thus caller_name cannot be __main__ –  Sridhar Ratnakumar Jul 8 '09 at 0:36
Are you sure your example code is correct? Shouldn't the body of myapp/bar.py be: import foo; foo.info('Hello') # => [myapp.bar] Hello –  too much php Jul 8 '09 at 0:38
Right, I fixed the code. –  Sridhar Ratnakumar Jul 8 '09 at 0:47
add comment

3 Answers

up vote 52 down vote accepted

Check out the inspect module:

inspect.stack() will return the stack information.

Inside a function, inspect.stack()[1] will return your caller's stack. From there, you can get more information about the caller's function name, module, etc.

See the docs for details:


Also, Doug Hellmann has a nice writeup of the inspect module in his PyMOTW series:


EDIT: Here's some code which does what you want, I think:

def info(msg):
    frm = inspect.stack()[1]
    mod = inspect.getmodule(frm[0])
    print '[%s] %s' % (mod.__name__, msg)
share|improve this answer
So how do you get the __name__ attribute of this module using the inspect module? For example, how do I get back myapp.foo (not myapp/foo.py) in my above example? I already tried using the inspect module before posting at SO. –  Sridhar Ratnakumar Jul 8 '09 at 3:38
Just updated the answer. Does that work on your end? –  ars Jul 8 '09 at 4:12
Be aware that this will interact strangely with import hooks, won't work on ironpython, and may behave in surprising ways on jython. It's best if you can avoid magic like this. –  Glyph Jul 9 '09 at 11:24
Also note that keeping a reference to a stack frame can prevent Python's GC from working correctly. See warning here: docs.python.org/library/inspect.html#the-interpreter-stack –  Kamil Kisiel Nov 9 '10 at 21:58
Note that if the caller function is decorated (@...), you need to access inspect.stack()[2] for the real caller. –  Amir Ali Akbari Feb 3 '13 at 8:19
add comment

Confronted with a similar problem, I have found that sys._current_frames() from the sys module contains interesting information that can help you, without the need to import inspect, at least in specific use cases.

>>> sys._current_frames()
{4052: <frame object at 0x03200C98>}

You can then "move up" using f_back :

>>> f = sys._current_frames().values()[0]

>>> print f.f_back.f_globals['__file__']

>>> print f.f_back.f_globals['__name__']

For the filename you can also use f.f_back.f_code.co_filename, as suggested by Mark Roddy above. I am not sure of the limits and caveats of this method (multiple threads will most likely be a problem) but I intend to use it in my case.

share|improve this answer
add comment

I don't recommend do this, but you can accomplish your goal with the following method:

def caller_name():
    return code.co_filename

Then update your existing method as follows:

def info(msg):
    caller = caller_name()
    print '[%s] %s' % (caller, msg)
share|improve this answer
Filename is not the same as __name__ –  Sridhar Ratnakumar Jul 8 '09 at 3:28
add comment

Your Answer


