py2exe編譯後,如何取得exe的絕對路徑
#!usr/bin/python<br />#coding = utf-8<br />import sys<br />import os<br />def main_is_frozen():<br /> """Return ''True'' if we're running from a frozen program."""<br /> import imp<br /> return (<br /> # new py2exe<br /> hasattr(sys, "frozen") or<br /> # tools/freeze<br /> imp.is_frozen("__main__"))<br />def get_main_dir():<br /> """Return the script directory - whether we're frozen or not."""<br /> if main_is_frozen():<br /> return os.path.abspath(os.path.dirname(sys.executable))<br /> return os.path.abspath(os.path.dirname(sys.argv[0]))<br />print get_main_dir()<br />
官網的解決方案
http://www.py2exe.org/index.cgi/WhereAmI
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
上面的連結
See also WhereAmI for another take on this problem.
ThomasHeller
posted this tip to the mailing list:
main_is_frozen()
returns
True
when running the exe, and
False
when
running from a script.
get_main_dir()
returns the directory name of the script or the directory
name of the exe - this is also sometimes useful.
切換行號顯示
1 import imp, os, sys
2
3 def main_is_frozen():
4 return (hasattr(sys, "frozen") or # new py2exe
5 hasattr(sys, "importers") # old py2exe
6 or imp.is_frozen("__main__")) # tools/freeze
7
8 def get_main_dir():
9 if main_is_frozen():
10 return os.path.dirname(sys.executable)
11 return os.path.dirname(sys.argv[0])
--結束連結
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
"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.getfilesystemenco