如何在 py2exe 中获取可执行文件的当前目录? [英] how can i get the executable's current directory in py2exe?
问题描述
我在我的脚本中使用这段代码以跨平台的方式精确定位它的运行位置:
I use this bit of code in my script to pinpoint, in a cross-platform way, where exactly it's being run from:
SCRIPT_ROOT = os.path.dirname(os.path.realpath(__file__))
很简单.然后我继续在脚本的其他区域使用 SCRIPT_ROOT
以确保所有内容都是正确的.当我通过 py2exe 运行它时,我的问题出现了,因为生成的可执行文件没有设置 __file__
,因此我的脚本中断了.有谁知道如何解决或解决这个问题?
Pretty simple. I then go on to use SCRIPT_ROOT
in other areas of my script to make sure everything is properly relative. My problem occurs when I run it through py2exe, because the generated executable doesn't set __file__
, therefore my script breaks. Does anyone know how to fix or work around this?
推荐答案
这里是 py2exe 文档参考 以下是相关项目:
Here is the py2exe documentation reference and here are the relevant items:
sys.executable
设置为 exe 文件的完整路径名.sys.argv
中的第一项是可执行文件的完整路径名,其余的是命令行参数.sys.frozen
只存在于可执行文件中.对于控制台可执行文件,它被设置为console_exe",对于无控制台 gui 可执行文件,它被设置为windows_exe",对于进程内的 dll 服务器,它被设置为dll".__file__
未定义(您可能想使用 sys.argv[0] 代替)
sys.executable
is set to the full pathname of the exe-file.- The first item in
sys.argv
is the full pathname of the executable, the rest are the command line arguments. sys.frozen
only exists in the executable. It is set to "console_exe" for a console executable, to "windows_exe" for a console-less gui executable, and to "dll" for a inprocess dll server.__file__
is not defined (you might want to use sys.argv[0] instead)
从这些文档中看不出exe 文件"和可执行文件"是否是同一回事,因此 sys.executable
和 sys.argv[0] 是否相同
是一回事.查看上次我必须执行此操作时对 script.py 和 py2exe_executable.exe 都有效的代码,我发现如下内容:
It is not apparent from those docs whether "the exe-file" and "the executable" are the same thing, and thus whether sys.executable
and sys.argv[0]
are the same thing. Looking at code that worked for both script.py and py2exe_executable.exe last time I had to do this, I find something like:
if hasattr(sys, 'frozen'):
basis = sys.executable
else:
basis = sys.argv[0]
required_folder = os.path.split(basis)[0]
正如我所说的那样有效,但我不记得为什么我认为这是必要的,而不是仅仅使用 sys.argv[0]
.
As I say that worked, but I don't recall why I thought that was necessary instead of just using sys.argv[0]
.
仅使用 basis
足以完成手头的工作(读取该目录中的文件).要获得更永久的记录,请拆分类似 os.path.realpath(basis)
的内容.
Using only basis
was adequate for the job in hand (read files in that directory). For a more permanent record, split something like os.path.realpath(basis)
.
更新 其实做了一个测试;胜过猜测和扶手椅式的夸夸其谈:-)
Update Actually did a test; beats guesswork and armchair pontification :-)
总结:忽略sys.frozen,忽略sys.executable,无条件使用sys.argv[0].
证据:
=== foo.py ===
=== foo.py ===
# coding: ascii
import sys, os.path
print 'sys has frozen:', hasattr(sys, 'frozen')
print 'using sys.executable:', repr(os.path.dirname(os.path.realpath(sys.executable)))
print 'using sys.argv[0]:', repr(os.path.dirname(os.path.realpath(sys.argv[0] )))
=== setup.py ===
=== setup.py ===
from distutils.core import setup
import py2exe
setup(console=['foo.py'])
=== 结果 ===
C:\junk\so\py2exe>\python26\python foo.py
sys has frozen: False
using sys.executable: 'C:\\python26'
using sys.argv[0]: 'C:\\junk\\so\\py2exe' # where foo.py lives
C:\junk\so\py2exe>dist\foo
sys has frozen: True
using sys.executable: 'C:\\junk\\so\\py2exe\\dist'
using sys.argv[0]: 'C:\\junk\\so\\py2exe\\dist' # where foo.exe lives
这篇关于如何在 py2exe 中获取可执行文件的当前目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!