【问题标题】:Trouble running a compiled app which includes singleton运行包含单例的已编译应用程序时遇到问题
【发布时间】:2012-09-10 23:15:56
【问题描述】:

我在运行包含单例的已编译应用程序时遇到问题,.pyw 编译过程运行良好,但是当我尝试运行生成的 .exe 时,它​​会写入错误日志,并显示如下消息:

Traceback (most recent call last):
  File "main.pyw", line 16, in <module>
  File "tendo\singleton.pyc", line 20, in __init__
AttributeError: 'module' object has no attribute '__file__'

这就是我所说的单例:

from tendo import singleton
me = singleton.SingleInstance()

【问题讨论】:

  • 为了更具体,请也输入您的代码
  • 你是如何“编译”这个应用程序的? py2exe?还有什么?它在打包到 .exe 文件之前是否有效?
  • @Senderle:我正在使用 py2exe 使 windows 可执行;
  • @GodMan:代码太长了,反正我不包括tendo的话,我可以用app。但是必须使用单例,因为我不能允许超过 1 个应用程序实例。

标签: python singleton pyqt pyqt4 py2exe


【解决方案1】:

sys.modules['__main__'].__file__tendo的单例模块makes use找到主目录。在 py2exe 中它不存在,这就是您收到此错误的原因。

您可以使用this answer 修复它。在tendo/singleton.py 的第 20 行,你有:

self.lockfile = os.path.normpath(tempfile.gettempdir() + '/' +
  os.path.splitext(os.path.abspath(sys.modules['__main__'].__file__))[0] \
  .replace("/","-").replace(":","").replace("\\","-") + '-%s' % flavor_id +'.lock')

替换成这样的:

path_to_script = get_main_dir()  #see linkd answer
self.lockfile = os.path.normpath(tempfile.gettempdir() + '/' +  path_to_script
  .replace("/","-").replace(":","").replace("\\","-") + '-%s' % flavor_id +'.lock')

将此作为问题报告给作者,和/或提出修复请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 2022-06-10
    相关资源
    最近更新 更多