Differences between revisions 1 and 2
Revision 1 as of 2006-05-10 09:04:39
Size: 975
Editor: cache2-2-ffm-vpn
Comment:
Revision 2 as of 2006-06-14 11:42:36
Size: 987
Editor: www
Comment: Fix markup for __file__
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
You cannot rely on __FILE__, because __FILE__ is not there in the py2exed main-script. You cannot rely on {{{__file__}}}, because {{{__file__}}} is not there in the py2exed main-script.

You want to know WHERE your .exe is

Why? Maybe, you are building a website and have a subdir ./static below the directory in which your .exe resides, from where you server all .css and .js that is static. Or, you put the logs right below the programs dir within ./log

Problem

You cannot rely on __file__, because __file__ is not there in the py2exed main-script. You could try to rely on ".", the "Current Directory", but that only works if your application was started there. That may happen, but it is not guaranted.

Solution

import os
import jpath

if hasattr(sys,"frozen") and sys.frozen == "windows_exe":
    p=jpath.path(os.path.abspath(sys.executable)).dirname()

now p contains the directory where your exe resides, no matter from where your exe has been called (maybe it is in the path)

"jpath" is the famous path module from [http://www.jorendorff.com/articles/python/path/ "Jason Orendorff"]

WhereAmI (last edited 2014-11-24 16:42:41 by JimmyRetzlaff)