0

I need to run my python app as windows service. I'm able to do that using commands,
python fservice.py install
python fservice.py start

Now, i want to create exe for my app using py2exe. I've followed code from this question: link

setup.py

from distutils.core import setup
import py2exe
import sys


if len(sys.argv) == 1:
    sys.argv.append("py2exe")
    sys.argv.append("-q")


class Target:
    def __init__(self, **kw):
        self.__dict__.update(kw)
        # for the versioninfo resources
        self.version = "0.0.1"
        self.company_name = "flotomate"
        self.copyright = "no copyright"
        self.name = "flotomate"

myservice = Target(
     # used for the versioninfo resource
     description = "flotomate",
     # what to build.  For a service, the module name (not the
     # filename) must be specified!
     modules = ["fservice"]
     )

setup(
     service = [myservice]
    )


fservice.py

import sys

import servicemanager
import win32serviceutil
import win32service
import win32event
import win32api
from pagent import app


class fservice(win32serviceutil.ServiceFramework):
    _svc_name_ = 'flotomate' #here is now the name you would input as an arg for instart
    _svc_display_name_ = 'flotomate' #arg for instart
    _svc_description_ = 'flotomate'# arg from instart

    def __init__(self, *args):
        win32serviceutil.ServiceFramework.__init__(self, *args)
        self.log('init')
        self.stop_event = win32event.CreateEvent(None, 0, 0, None)


        #logs into the system event log
    def log(self, msg):
        import servicemanager
        servicemanager.LogInfoMsg(str(msg))

    def sleep(self, minute):
            win32api.Sleep((minute*1000), True)

    def SvcDoRun(self):
        self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
        try:
            self.ReportServiceStatus(win32service.SERVICE_RUNNING)
            self.log('start')
            self.start()
            self.log('wait')
            win32event.WaitForSingleObject(self.stop_event, win32event.INFINITE)
            self.log('done')
        except Exception:
            self.SvcStop()

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        self.stop()
        win32event.SetEvent(self.stop_event)
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)

    def start(self):
        app.run(host='0.0.0.0',port=4999)

    # to be overridden
    def stop(self):
        pass


if __name__ == '__main__':
    if len(sys.argv) == 1:
        servicemanager.Initialize()
        servicemanager.PrepareToHostSingle(fservice)
        servicemanager.StartServiceCtrlDispatcher()
    else:
        win32serviceutil.HandleCommandLine(fservice)

I'm creating exe using command,
python setup.py py2exe

but, when i try to install the service using
fservice.exe --install

I get this error

Traceback (most recent call last):
  File "boot_service.py", line 37, in <module>
AttributeError: 'module' object has no attribute 'Initialize


boot_service.py of py2exe
I'm using Python 2.7.6 and py2exe-0.6.9

mr1holmes
  • 136
  • 7

1 Answers1

0

I encountered the same problem. I don't know whether you found the solution yet

In my case, the reason would be that servicemanager is not included in the compiled package. It seems the installed servicemanager library in python issues conflict.

To solve this problem, I uninstall servicemanager if it is unused or manually copy servicemanager.pyd to folder dist and servicemager.pyc to dist\library.zip. If there is a folder named servicemanager in dist\library.zip, just delete it.

If you already had a better solution, please share it ^^

bomlinh
  • 11
  • 1