RuntimeError: servicemanager.__file__ could not be loaded - win32 error code is 193
For all bugs, please provide the following information:
Expected behavior and actual behavior
install and start the service.
Event viewer log: The description for Event ID 14 from source Python Service cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.
If the event originated on another computer, the display information had to be saved with the event.
The following information was included with the event:
RuntimeError: servicemanager.file could not be loaded - win32 error code is 193
The specified resource type cannot be found in the image file
Steps to reproduce the problem
same behavior using pyinstaller
- python main.py install
- python main.py start
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import os
import time
class MyService(win32serviceutil.ServiceFramework):
_svc_name_ = 'MyService'
_svc_display_name_ = 'My Service'
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(120)
self.is_alive = True
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
self.is_alive = False
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.main()
def main(self):
# Main service logic goes here
while self.is_alive:
# Perform your service tasks here
time.sleep(5) # Example: Sleep for 5 seconds
if __name__ == '__main__':
if len(os.sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(MyService)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(MyService)
System information
Intel(R) Xeon(R) CPU E5-2650 v4 @ 2.20GHz 2.20 GHz Windows Server 2022 Standard 21H2
Python version and distribution: 3.12, 3.13
pywin32 version:
Windows Version:
DLL locations:
The event log not being registered correctly is annoying, but there's probably python exception details buried in that log event. Does "debug" work?
@mhammond when I run debug I get PythonService was unable to locate the service manager. Please see the event log for details and in the event log show the error in the description
Start command output:
Error starting service: The service did not respond to the start or control request in a timely fashion.
oops, sorry, I missed that info. Error 193 is ERROR_BAD_EXE_FORMAT and is coming from https://github.com/mhammond/pywin32/blob/0c7297b861b962a00e0a14a6d995f339663429e0/win32/src/PythonService.cpp#L1471. This seems very odd - I really can't come up with any ideas for how this might fail in this way, unless there's something strange like a servicemanager.py being found instead of the expected servicemanager.pyd.