Making services more like regular win32 apps

From, update your to look something like this:

myservice = Target(
    # used for the versioninfo resource
    description = "A sample Windows NT service",
    # what to build. For a service, the module name (not the
    # filename) must be specified!
    modules = ["MyService"],

    service = [myservice],

See samples\advanced\ for more details.

Note: using an object for setup() is a py2exe extension of distutils

The standard win32 commands for HandleCommandLine():

Without the cmdline_style parameter, you get this output from running your service in CMD:

Services are supposed to be run by the system after they have been installed.
These command line options are available for (de)installation:
        -user: <arg>
        -password: <arg>
Connecting to the Service Control Manager
Traceback (most recent call last):
  File "", line 173, in ?
pywintypes.error: (1063, 'StartServiceCtrlDispatcher', 'The service process could not connect to the service controller.')

In 'py2exe for Python 3.3 and later' 'cmdline_style' is not passed to the compile system, while it is defined in the setup-script.


<Python PATH>\Lib\site-packages\py2exe\ line 561
-                compile("cmdline_style = 'py2exe'; service_module_names = %r" % (target.modules,),
+                compile("cmdline_style = '%s'"%(getattr(target,"cmdline_style","py2exe")) + "; service_module_names = %r" % (target.modules,),

Things that can go wrong with py2exe and services

within demos/advanced there is an example to create windows services with py2exe. It works quite out of the box.

Difficulties arise later on - as soon as you wanna do something REAL with that service.

Error Message

Python could not import the service's module
  File "C:\ham\jds\", line 21, in ?
    import transinhalte
  File "C:\ham\jds\", line 17, in ?
    import unite
  File "C:\ham\jds\", line 9, in ?
    import version
exceptions.ImportError: dynamic module does not define init function (initversion)

yours might be different ... what went wrong????

Everything worked fine as long as I was running that service within debug mode. Then, starting it up as "real service", this error appeared.

a Mail of Gerald Haering showed me the light: when a service is started, all paths are TOTALLY DIFFERENT ANIMALS then in any other running state of python... so, the service tried to load version.dll, which is something that comes within windows/system32, and of course has NO Python init-code.


Make sure that NONE of your Python module names conflicts with ANY of the windows system dll names

py2exeAndWindowsServices (last edited 2015-12-07 09:41:17 by TijnGommans)