Get __name__ of calling function's module in Python
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.bar] 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? ars 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: