运行编译的Python脚本时出错
我用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。