How do I find out the file that is the beginning of a Python program? -


this question has answer here:

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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -