How to identify the name of the file which calls the function in python?
<p>I have a server.py which contains a function and other files like requestor1.py requestor2.py .... requestorN.py</p>

<p>Server.py contains a function :</p>

<pre><code>def callmeforhelp()
    return "I am here to help you out!"
</code></pre>

<p>and requestor1.py file calls the function callmeforhelp() and it has the imports needed to call the function from server.py</p>

<p>Is there a way my server.py knows which file is calling it?</p>

<p>Something similar like below :</p>

<p>When requestor1.py calls the function, then :</p>

<pre><code>def callmeforhelp()
    print "Now I am being called by : "+caller // caller must contain the value as requestor1.py or even full path of requestor1.py
    return "I am here to help you out!"
</code></pre>

<p>Here is a way to get at the caller's local attributes:</p>

<pre><code>import sys

def callmeforhelp():
    print("Called from", sys._getframe(1).f_locals['__file__'])
</code></pre>

<p>This is a feature of CPython and is not guaranteed to be present in other language implementations.</p>

python