python-3.xdjangowindows-serviceswaitressservicemanager

Module not found error in Python script running as a Windows Service


I have a Django "learning project" which hosts a very simple web site, with waitress providing the WSGI server. The project performs as expected when I execute the calling script from the command line.

From here I'm trying to set up the scaffolding to have it run as a Windows Service. I installed pywin32 and servicemanager to my virtual environment, and -- later on, trying to resolve an error -- also installed them to my "real" Python instance on the machine. Despite this, I keep getting a ModuleNotFoundError error, citing servicemanager, when I try to start the created service:

service start error

This is the Python script I'm using; it is based on a template script I found here.

import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
from waitress import serve
from django_project.wsgi import application

class AppServerSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "PagesService"
    _svc_display_name_ = "Pages Service"

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

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_,''))
        self.main()

    def main(self):
        serve(application, port='8000')

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(AppServerSvc)

As noted above I've installed servicemanager to both the virtual environment and the "real" environment, I've followed the classic Roy request and rebooted the machine just to be sure. Here's the requirements.txt file I get from pip freeze, it shows servicemanager is there:

argcomplete==1.12.3
asgiref==3.6.0
atomicwrites==1.4.1
attrs==23.1.0
bottle==0.12.18
certifi==2022.12.7
chardet==3.0.4
colorama==0.4.6
Django==4.1.8
idna==2.10
more-itertools==9.1.0
mssql-django==1.2
packaging==23.1
pluggy==0.13.1
prettytable==0.7.2
py==1.11.0
PyHamcrest==2.0.2
pymongo==3.11.0
pyodbc==4.0.39
pytest==5.4.3
pytz==2023.3
pywin32==306
requests==2.24.0
servicemanager==2.0.10
sqlparse==0.4.4
tzdata==2023.3
urllib3==1.25.11
waitress==2.1.2
wcwidth==0.2.6

So I can't understand why the servicemanager module wouldn't be found or what else I could do to make it available.


Solution

  • After a few more tests (read: shots in the dark) and doing some more extensive digging in Google with the resulting errors, I found this article on Github.

    The poster's issue was very similar, just with a different specific error. Owing to circumstances, the poster was able to pin it down to an incompatibility between pywin32 and Python 3.11 (since the issue started only when they upgraded from 3.10 to 3.11). The clincher was that the issue went away when they reverted to 3.10. (UPDATE, 2023-10-19: Issue is also triggered when using Python 3.12.)

    So I rebuilt my environment with the current 3.10 release (3.10.11), installed and ran the service -- and now it works.

    successful start