abisko
abisko

Reputation: 743

How to get the caller script full path in Python

I have a caller script /home/x/a/a.py that uses module /home/x/b.py

Inside b.py, I want to know the full path of the caller script, i.e. /home/x/a/a.py. How can I get that?

I looked at How to get the caller script name but it only gave me the script name which is a.py

Upvotes: 2

Views: 1580

Answers (1)

Iain Shelvington
Iain Shelvington

Reputation: 32244

A very simplified version of what happens (for the CPython interpreter):

Every time a method is called in Python a "frame" is added to the stack, after a method returns something the interpreter pops the last frame from the stack and continues execution of the previous frame with the return value injected in place of the method call

To get the previous frame you can call sys._getframe(1) (0 would get the current frame, 1 gets the previous frame). The inspect module provides a method getframeinfo that returns some useful information about the frame including the filename. This can be combined like so

import inspect
import sys

def foo():
    print('Called from', inspect.getframeinfo(sys._getframe(1)).filename)

Whenever foo is called it will print the filename of the calling method

Upvotes: 3

Related Questions