replacement for __file__ in compiled exe

John Machin sjmachin at
Mon Jan 5 21:51:31 CET 2009

On Jan 6, 7:03 am, TechieInsights <GDoerm... at> wrote:
> __file__ command does not work when compiled to exe.  It makes since
> because the file is now in a compressed library.  Is there a
> replacement or something else you can do?  The real problem is that
> when you create an exe of your program with python embedded, you can't
> always guarantee that your current directory is the directory of your
> program.

How can you *ever* guarantee that the current directory is the same as
the directory in which the program resides? This lack of guarantee is
quite independent of whether the "program" is .py, .exe, .bat, .com,
etc. Isn't the real problem how to find out which directory the
program is in?

>  I guess when you could just set a registry entry on
> windows... but it would be nice to have a quick fix for this (like
> os.path.dirname(__file__)).

    if hasattr(sys, 'frozen'):
        answer = os.path.split(sys.executable)[0]
        answer = os.path.split(sys.argv[0])[0]

(maybe) ... your question is a little unclear.


More information about the Python-list mailing list