Search code examples
pythonconfigparser

How do I get the location of the entry module in python?


I know I can use __file__ and os.path.* to figure out the path of the current module. What about the entry module?

I'm trying to load an ini file into ConfigParser from a module based on the hostname, but I want it to be relative to the entry assembly, not necessarily the working directory. Is this possible?


Solution

  • If by entry module you mean the program's entrypoint this is usually sys.argv[0]. So you could do something like this:

    Example:

    import os
    import sys
    
    
    def main():
        basedir = os.path.dirname(sys.argv[0])
        print "base directory: {0:s}".format(basedir)
    
    
    if __name__ == "__main__":
        main()