Differences between revisions 9 and 10
Revision 9 as of 2010-08-04 20:14:12
Size: 2256
Editor: denversc
Comment:
Revision 10 as of 2014-11-24 05:21:59
Size: 339
Editor: SMRWO
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
##language:en
== 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 HowToDetermineIfRunningFromExe 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 [[http://www.jorendorff.com/articles/python/path/|"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( )))

}}}
Greetings! I am Paulita we totally dig that reputation. She is a place of work supervisor. Idaho is where we've been living institutions and individuals and I do not plan on changing it's. He is really fond of playing badminton but he doesn't notice the time now. See what's new on my website here: [[http://icargames.net|Icargames.net]]

Greetings! I am Paulita we totally dig that reputation. She is a place of work supervisor. Idaho is where we've been living institutions and individuals and I do not plan on changing it's. He is really fond of playing badminton but he doesn't notice the time now. See what's new on my website here: Icargames.net

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