Search code examples
pythonsingletonpyqtpyqt4py2exe

Trouble running a compiled app which includes singleton


I'm having trouble running a compiled app which includes singleton, the .pyw compilation proccess goes just fine, but when I attempt to run the resulting .exe, it writes an error log with the message shown below:

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__'

this is how I'm calling singleton:

from tendo import singleton
me = singleton.SingleInstance()

Solution

  • The tendo's singleton module makes use of sys.modules['__main__'].__file__ to find the main directory. In py2exe it doesn't exist, that's why you get this error.

    You can fix it with this answer. In tendo/singleton.py, line 20 you have:

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

    Replace with something like this:

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

    Report this as an issue to the author, and/or make a pull request with the fix.