【问题标题】:Problem running compiled Python script [duplicate]运行已编译的 Python 脚本时出现问题 [重复]
【发布时间】:2010-11-15 20:52:52
【问题描述】:

所以我根据this answer用py2exe编译了一个python脚本。编译过程中没有错误,一切正常。

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

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" )

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

【问题讨论】:

    标签: python


    【解决方案1】:

    __file__ 在 py2exe 中不起作用。这是因为该模块位于 .exe 中,因此无需设置 __file__ 即可为您提供 python 文件。

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

    【讨论】:

      猜你喜欢
      • 2011-05-31
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      相关资源
      最近更新 更多