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>Try it in your <code>server</code> file:</p>

<pre><code>import inspect

def callmeforhelp():
    result = inspect.getouterframes(inspect.currentframe(), 2)
    print("Caller is: " + str(result[1][1]))
</code></pre>

python