运行编译后的Python脚本出现问题

4

我已经按照这个答案的方法,使用py2exe对Python脚本进行了编译。在编译过程中没有出现任何错误,一切都很顺利。

当我像这样从命令行运行脚本时:

C:\Users\Richard\Dist\backprop3.exe 60

这是我得到的输出:
C:\Users\Richard>C:\Users\Richard\Dist\backprop3.exe 60
Traceback (most recent call last):
  File "backprop3.py", line 209, in <module>
  File "backprop3.py", line 175, in demo
NameError: global name '__file__' is not defined

C:\Users\Richard>

这是指的这行代码:

image = Image.open( os.path.dirname( os.path.abspath( __file__ ) )+"/backprop-input.bmp" )

那行代码只是从当前目录加载一张图片。问题出在哪里?
1个回答

8

__file__ 在 py2exe 中无法使用。这是因为模块位于 .exe 内部,因此没有任何可以设置 __file__ 的内容,这将为您提供 Python 文件。

请参见http://www.py2exe.org/index.cgi/WhereAmI,了解处理此问题的技巧。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接