py2exe编译后,如何取得exe的绝对路径

py2exe编译后,如何取得exe的绝对路径

 

 

 

官网的解决方案

 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值