Martin Rezyne
Martin Rezyne

Reputation: 445

Python: Get file path of the scripts which imports a module

I want to make a module and to find the path of the scripts which import my module. For example I have a module: myscript.py which is imported by test.py. I want to print from myscript.py, the path to test.py file

How can i do this? I have read about __ file __, but it doesn't really work the way i imagined it will work.

myscript.py

def printName():
    print __file__

test.py - i call this script

import myscript
myscript.printName()

I want to print test.py but it prints myscript.py

Upvotes: 0

Views: 85

Answers (1)

ILostMySpoon
ILostMySpoon

Reputation: 2409

import __main__

def printName():
    print __main__.__file__

Upvotes: 1

Related Questions