Locating the executable¶
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
See also Detecting frozen programs for a shorter recipe
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 guaranteed.
Solution¶
import os
import jpath
if hasattr(sys,"frozen") and sys.frozen in ("windows_exe", "console_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 "Jason Orendorff"
Alternate Solution¶
The solution above will fail with a UnicodeDecodeError when using a Japanese
(or Chinese/Korean, probably) version of Windows (XP + others?), and the path
contains double-byte characters. This is because the sys.executable is in the
file system encoding ('mbcs' on WinXP Japanese), and python tries to decode it
to Unicode using the default Python encoding ('ascii'). The solution is to
explicitly convert the path to Unicode using the default file system encoding.
Additionally, checking only for a value of "windows_exe" will fail for a
console application. I decided to live dangerously, and just test for "frozen"
:-).
import os
import sys
def we_are_frozen():
"""Returns whether we are frozen via py2exe.
This will affect how we find out where we are located."""
return hasattr(sys, "frozen")
def module_path():
""" This will get us the program's directory,
even if we are frozen using py2exe"""
if we_are_frozen():
return os.path.dirname(unicode(sys.executable, sys.getfilesystemencoding( )))
return os.path.dirname(unicode(__file__, sys.getfilesystemencoding( )))