Let's assume that I have a program that consists of at least two files, magic.py which is part of the herebemagic module...
def foo():
# This line is what my question will be all about
pass
...and main.py, which starts the program:
import os
from herebemagic import magic
print("This is where this file lies: %s" % (repr(os.path.abspath(__file__)), ))
magic.foo()
As demonstrated, finding a module's file, and the path that it is in, is trivial. But how would I, in magic.py, get a reference to main.py's module (being the one that was invoked by the interpreter, not necessarily the one that imported herebemagic directly), so that I can get at its file?
Of course I could just figure it out in main.py, and pass it down to foo(), but in practice there can be an arbitrary number of modules in between the two, and in my case, it'd be ugly to pass it down (I'm loading a lot of modules with importlib, and the information is currently only relevant to one of the modules). Or I could use a builtin, but that is a namespace that no one wants to litter.