win32serviceutil.ServiceFrameworkclass in his book Python Programming on Win32.
The problem is, that if you use a breakpoint in your service code and try to debug it, you'll find that it never stops at your breakpoint. The reason is that the
win32serviceutilmodule will actually run PythonService.exe and have that program run your python script. Since PythonService.exe is its own process that has no knowledge of your IDE's breakpoints, it just runs without breaking.
Here is a simple trick for the small intersection of people who:
- Write Windows Services
- in Python
- using an IDE (I use pyDev)
- and need to step through their code to debug.
It turns out that
win32serviceutilhas a reimplementation of the logic in PythonService.exe that it uses to emulate PythonService.exe's debug mode when you convert your python script you an executable (e.g. via py2exe). The implementation is in
DebugService()function. The trick to being able to set breakpoints and debug your python service is to convince
DebugServicerather than spawning PythonService.exe.
Luckily, this is trivially easy: just add the line
sys.frozen = 'windows_exe' # Fake py2exe so we can debug
before you call
Then, you just need to pass the '-debug' command-line argument when you run your service to force it into debugging mode. Your debugger should then control the process so you can debug it.