Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to write a windows service using Python?

Tags:

python

windows

Python has a win32service package that seems to allow windows service creation. I have carefully checked available google examples, for example this one: Is it possible to run a Python script as a service in Windows? If possible, how?

I have placed code into ~/Documents/test.py and executed following under elevated command prompt:

> python test.py install
> python test.py debug

Unfortunately, every example i tried fails with error:

Debugging service TestService - press Ctrl+C to stop.
Error 0xC0000005 - Python could find the service class in the module

AttributeError: 'module' object has no attribute 'AppServerSvc'

(null): (null)

I have tested it with latest version of ActivePython (2.7.2.5 32-bit) on Windows 7 64-bit and Windows 8 64-bit. Of course, if service is started maually via service manager, it will not start either.

Am i doing something wrong or Python is not intended to be used for service creation on modern operating systems? Maybe i need a specific version of windows/python/pywin32? Of course, i can manually test all combinations starting from windows XP but it will take lots of time :(. Maybe someone already has experience with Python and windows services and can hint me what i'm doing wrong?

update

Tested on Windows XP 32-bit virtual macine (same code, same activepython distribution) - same error.

like image 635
grigoryvp Avatar asked Nov 13 '22 18:11

grigoryvp


1 Answers

It appears that there is a bug in the ActiveState build for PyWin32. Installing ActiveState python 2.7.2.5 and running the linked sample code above, I get the same error that you are reporting.

But if I download Python 2.7.3 (2.7.2.5 does not seem to be available for download) and add PyWin32 build 214 (ActiveState seems to be using the 214 version of PyWin32). Then everything seems to work just fine. I also tried the latest build of PyWin32 (218) and it also worked correctly.

So I guess you can try reporting the problem to ActiveState (I don't have a support contract with them) and unless you have a requirement for using ActiveState, you can just switch to the standard Python builds.

I have been using services with the standard Python builds for years running on everything from Windows 2000 up to Server 2008 and Windows 7 with no problems. So I have good reason to believe that it will work for you also.

If you want to work with ActiveState to get the problem fixed, then the bug appears to be in their build of PythonService.cpp in the LoadPythonServiceInstance function. I looked at the registry entries that were created and they look fine, it is the PythonService.exe that is failing at loading your class. Based on the error message it appears to have loaded the module correctly and is just having trouble finding the class.

like image 173
JimP Avatar answered Nov 15 '22 12:11

JimP