Python win32serviceutil ModuleNotFoundError

279 Views Asked by At

I created a Python win32serviceutil service following the excellent guide at https://thepythoncorner.com/posts/2018-08-01-how-to-create-a-windows-service-in-python/.

It will install just fine

C:\›python PowerMasterUPS.py install
Installing service PowerMasterUPS
copying host exe 'C:\Users\x\AppData\Local\Programs\Python\Python311\Lib\site-packa
C:\Users\x\AppData\Local\Programs\Python\Python311\pythonservice.exe'
Service installed

But it fails to start

C:\›python PowerMasterUPS.py debug
Debugging service PowerMasterUPS - press Ctrl+C to stop.
Error (xC0000004 - Python could not import the service's module

ModuleNotFoundError: No module named 'PowerMasterUPS'

(null): (null)

I tried to add sys.path.append("C:\\") but still the same error. This is the directory where the PowerMasterUPS.py is.

import socket
import win32serviceutil
import servicemanager
import win32event
import win32service
import requests ,time ,json, os, sys
sys.path.append("C:\\")

class SMWinservice(win32serviceutil.ServiceFramework):
    '''
    SMWinservice
    by Davide Mastromatteo
    '''

    _svc_name_ = 'pythonService'
    _svc_display_name_ = 'Python Service'
    _svc_description_ = 'Python Service Description'

    @classmethod
    def parse_command_line(cls):
        '''
        ClassMethod to parse the command line
        '''
        win32serviceutil.HandleCommandLine(cls)

    def __init__(self, args):
        '''
        Constructor of the winservice
        '''
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
        socket.setdefaulttimeout(60)

    def SvcStop(self):
        '''
        Called when the service is asked to stop
        '''
        self.stop()
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        '''
        Called when the service is asked to start
        '''
        self.start()
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_, ''))
        self.main()

    def start(self):
        '''
        Override to add logic before the start
        eg. running condition
        '''
        pass

    def stop(self):
        '''
        Override to add logic before the stop
        eg. invalidating running condition
        '''
        pass

    def main(self):
        '''
        Main class to be ovverridden to add logic
        '''
        pass

class PowerMasterUPS(SMWinservice):
    _svc_name_ = "PowerMasterUPS"
    _svc_display_name_ = "PowerMasterUPS"
    _svc_description_ = "PowerMasterUPS will Shutdown Windows when UPS battery is depleting"

    def start(self):
        self.isrunning = True

    def stop(self):
        self.isrunning = False

    def main(self):
        while self.isrunning:
            # my app code here

if __name__ == '__main__':
    PowerMasterUPS.parse_command_line()
0

There are 0 best solutions below