5

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.

Community
  • 1
  • 1
grigoryvp
  • 40,413
  • 64
  • 174
  • 277
  • 3
    I'm almost certain that the error you get is a trivial coding error related to module locations. Please post a [reproducible example](http://sscce.org) of the code that initializes `AppServerSvc`. Also, the obvious answer to the title is yes, and you even link to the duplicate. – phihag Nov 01 '12 at 13:32
  • @phihag I posted command line used to reproduce problem and complete source code alongside with operating system version. What other information do you think is needed for "reproducible example"? – grigoryvp Nov 01 '12 at 14:21
  • A reproducible example is the full code that anyone can run to, well, *reproduce* your problem (i.e. enough code so that *I* can type `python test.py debug`). In your case, it's at least `test.py`, plus the file where you have stored defined `class AppServerSvc`. – phihag Nov 01 '12 at 16:47
  • @phihag There is a hyperlink to complete source code. Quest explicitly states that i have copied source code from hyperlink into 'test.py'. I don't want to copypaste this code into question, sorry. – grigoryvp Nov 01 '12 at 17:25
  • 1
    Sorry, the only link I see is one to an [answer](http://stackoverflow.com/questions/32404/can-i-run-a-python-script-as-a-service-in-windows-how). This question just states that you *placed (some) code*, but I presume you copied the contents of [this answer](http://stackoverflow.com/a/32440/35070) to a file. Notably, you don't mention which EOL scheme you've used. Additionally, I'm not certain what you mean by `Unfortunately, every example i tried fails` - does that include `python test.py install` or is the code block before the literal output you get? – phihag Nov 01 '12 at 18:54
  • @phihag I have tried many python service example found over the google. They are not working with same error, so it's something i'm doing wrong. And it's not a line ending issue :) – grigoryvp Nov 01 '12 at 23:51
  • I think you'll have to show some code, especially around where the AppServerSvc is being used. – Keith Nov 03 '12 at 22:51
  • 2
    Show the code, guy! Point to a concrete, complete example that fails for you. What you think is obvious is not clear to everyone, and may not even be true. – alexis Nov 03 '12 at 23:16

1 Answers1

1

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.

JimP
  • 406
  • 2
  • 8
  • could you please tell me if it is possible to use the multiprocessing.Manager() within a windows service? I am having a number of issues getting multiprocessing working. – Matt Seymour Jan 07 '13 at 15:35
  • It should be possible, but I don't have any personal experience under windows with multiprocessing. I'm sure if you ask a new question with details of what you are trying to accomplish that we (the community) can help. – JimP Jan 08 '13 at 02:12