How do I find out the file that is the beginning of a Python program? -
this question has answer here:
- python - find path file being run 6 answers
let's assume have program consists of @ least 2 files, magic.py part of herebemagic module...
def foo(): # line question pass
...and main.py, starts program:
import os herebemagic import magic print("this file lies: %s" % (repr(os.path.abspath(__file__)), )) magic.foo()
as demonstrated, finding module's file, , path in, trivial. how i, in magic.py, reference main.py's module (being 1 invoked interpreter, not 1 imported herebemagic directly), can @ file?
of course figure out in main.py, , pass down foo(), in practice there can arbitrary number of modules in between two, , in case, it'd ugly pass down (i'm loading lot of modules importlib, , information relevant 1 of modules). or use builtin, namespace no 1 wants litter.
the python executable called name of main script command line argument. python keeps in sys.argv
:
sys.argv[0]
Comments
Post a Comment