title: Python 3 executable as windows service
body: <p>I'm trying to write a windows Service in python, but the tricky part is i want to deploy it on a machine that doesn't have python.
  I've successfully created a service like <a href="https://stackoverflow.com/questions/32404/can-i-run-a-python-script-as-a-service-in-windows-how">this</a>, and it works if i run from my machine. the problem starts when i try to convert it to an exe and then try to install it.
  first i tried to use cx_freeze service example, (seen <a href="https://bitbucket.org/anthony_tuininga/cx_freeze/src/4fa5dfdaae24/samples/service/?at=default" rel="noreferrer">here</a>), the setup.py look like this :</p>
  
  <pre><code>from cx_Freeze import setup, Executable
  
  options = {'build_exe': {'includes': ['ServiceHandler']}}
  
  executables = [Executable('Config.py', base='Win32Service', targetName='gsr.exe')]
  
  setup(name='GSR',
      version='0.1',
      description='GSR SERVICE',
      executables=executables,
      options=options
      )
  </code></pre>
  
  <p>and config.py is:</p>
  
  <pre><code>NAME = 'GSR_%s'
  DISPLAY_NAME = 'GSR TEST - %s'
  MODULE_NAME = 'ServiceHandler'
  CLASS_NAME = 'Handler'
  DESCRIPTION = 'Sample service description'
  AUTO_START = True
  SESSION_CHANGES = False
  </code></pre>
  
  <p>but when i try to build it (python setup.py build) i get an error:
  "cx_Freeze.freezer.ConfigError: no base named Win32Service"</p>
  
  <p>Second, i tried using a regular cx_freeze setup, the exe i get installs the service fine but once i try to start it i get an error:
  "Error 1053: The service did not respond to the start or control request in a timely fashion"</p>
  
  <p>setup.py - python 3.3 regualr exe, installs the service but when trying to start it sends an error:</p>
  
  <pre><code>from cx_Freeze import setup, Executable
  
  packages = ['win32serviceutil','win32service','win32event','servicemanager','socket','win32timezone','cx_Logging','ServiceHandler']
  build_exe_options = {"packages": packages}
  executable = [Executable("ServiceHandler.py")]
  
  
  setup(  name = "GSR_test",
          version = "1.0",
          description = "GSR test service",
          options = {"build_exe": build_exe_options},
          executables = executable)
  </code></pre>
  
  <p>finally, I managed to get it to work in python 2.7 using py2exe, but py2exe isn't available for python 3.3 and I my code is in 3.3</p>
  
  <p>i guess the problem is in the configuration of the setup.py im using with cx_freeze. 
  any ideas ??</p>
  
  <p>my ServiceHandler:</p>
  
  <pre><code>import pythoncom
  import win32serviceutil
  import win32service
  import win32event
  import servicemanager
  import socket
  from test import test
  import threading
  
  class AppServerSvc (win32serviceutil.ServiceFramework):
      _svc_name_ = "GSR_test"
      _svc_display_name_ = "GSR test Service"
  
      def __init__(self,args):
          win32serviceutil.ServiceFramework.__init__(self,args)
          self.hWaitStop = win32event.CreateEvent(None,0,0,None)
          socket.setdefaulttimeout(60)
          self.app = test()
          self.flag = threading.Event()
  
      def SvcStop(self):
          self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
          win32event.SetEvent(self.hWaitStop)
          self.flag.set()
  
      def SvcDoRun(self):
          servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                                servicemanager.PYS_SERVICE_STARTED,
                                (self._svc_name_,''))
          self.main()
  
      def main(self):
          t = threading.Thread(target=self.app.run)
          t.start()
          self.flag.wait()
          raise SystemExit
  
  if __name__ == '__main__':
      win32serviceutil.HandleCommandLine(AppServerSvc)
  </code></pre>
  
  <p>Setup.py , python 2.7 using py2exe that works: (taken from <a href="https://www.evanreichard.com/?p=86" rel="noreferrer">here</a>)</p>
  
  <pre><code>from distutils.core import setup
  import py2exe
  
  setup(  service = ["ServiceHandler"],
          description = "SERVICE TEST",
          modules = ["GSR_test"],
          cmdline_style='pywin32', ) 
  </code></pre>
  
  <p>Thanks,
  Amit</p>