运行编译的Python脚本时出现问

Ric*_*nop 4 python

所以我根据这个答案用py2exe编译了一个python脚本.编译过程中没有错误,一切都很顺利.

当我从这样的cmd运行脚本时:

C:\Users\Richard\Dist\backprop3.exe 60
Run Code Online (Sandbox Code Playgroud)

这是我得到的输出:

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>
Run Code Online (Sandbox Code Playgroud)

这是指这一行:

image = Image.open( os.path.dirname( os.path.abspath( __file__ ) )+"/backprop-input.bmp" )
Run Code Online (Sandbox Code Playgroud)

该行只是从当前目录加载图像.问题出在哪儿?

Win*_*ert 8

__file__将无法在py2exe中工作.这是因为模块位于.exe内部,因此没有任何设置__file__可以为您提供python文件.

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