Skip to main content
 首页 » 编程设计

python之使用 py2exe 时出现 NameError

2024年11月01日8exmyth

<分区>

我正在编写一个程序,它使用 os.path.abspath(os.path.dirname(__file__))) 来定位它的配置文件,当我在纯 Python,但是一旦我使用 py2exe 将它编译为 .exe,我就会收到此错误:

Traceback (most recent call last): 
  File (main.py, line 17, in <module> 
NameError: name '__file__' is not defined 

确切地说,第 17 行是:

if os.path.isfile("%s/config.cfg" % os.path.abspath(os.path.dirname(__file__))): 

为什么会发生这种情况,我该如何克服这个问题?

提前致谢! :)