'Python how to return filename of script that will use class

Imagine you have a class with path project/my_class.py:

class MyClass:
    def __init__(self):
        name = __file__

    def print_file():
        print(self.name)

Currently the __file__ value will return project/my_class.py.

What I'm trying to figure out if at all possible is if I were to import this class, how can I return the script that's calling this class' __file__ value in the class itself?

Example project/my_example.py:

from my_class import MyClass
MyClass().print_file()

But this will return project/my_class.py. What function or variable would I need to return the expected project/my_example.py?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source