Reputation: 6241
Consider you have have a script called /home/user/pack/test.py
Within this script we call some helper class which is defined in other file path(using import)
Lets say the Helper file path is:
/home/user/pack/abc/helper.py
I wonder if there is a way to get the original launching script path /home/user/src/test.py
within the Helper script for some usage.
I tried using __file__
but it gave me /home/user/pack/abc/helper.py
of course.
Note: I don't want to pass the launching script path(__file__) as parameter to the Helper class.
Upvotes: 2
Views: 514
Reputation: 917
You can use the inspect
module from the Standard Library to achieve your goal, this way:
helper.py
def some_helper():
print(inspect.stack()[1][1])
test.py
import helper
helper.some_helper()
So by executing test.py
you will get "path/to/test.py"
as your output inside the helper.py
function.
Upvotes: 3