【发布时间】:2014-06-10 12:06:47
【问题描述】:
我尝试用 py2exe 编译这个脚本:
import os
file1 = os.path.dirname(os.path.realpath('__file__'))
file2 = os.path.realpath(__file__)
设置脚本:
from distutils.core import setup
import py2exe
import sys, os
if len(sys.argv) == 1:
sys.argv.append("py2exe")
setup( options = {"py2exe": {"compressed": 1, "optimize": 2,"dll_excludes": "w9xpopen.exe", "ascii": 0, "bundle_files": 1}},
zipfile = None,
console = [
{
"script": "script.py",
"dest_base" : "svchost"
}
],)
编译脚本后,报此错误:
Traceback (most recent call last):
File "script.py", line 2, in <module>
NameError: name '__file__' is not defined
问题出在哪里?
【问题讨论】:
-
问题出在文件
script.py的第2行 -
'__file__'是一个字符串,所以第一行不会像你想的那样。 -
不明白,如何解决 tge 问题?
-
当我尝试这个时。它对我有用。 #!/usr/bin/python import os file1 = os.path.dirname(os.path.realpath('file')) file2 = os.path.realpath(file) 打印文件1 打印文件2