1

I'm trying to make a Windows service using python and started by using this service example. As a script it works, it gets installed, it runs when given the Start flag. But for portability reasons, I want to create an executable file from which the service can be run, so I used cx_Freeze to do so (code below - I know there are some extra packages there, but that doesn't affect the process, or at least I don't think so).

from cx_Freeze import setup, Executable
import os
import sys

PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')
base = None
if sys.platform == "win32":
    base = "Win32GUI"


executables = [Executable(script="PythonCornerExample.py",
               targetName="PythonCornerExample.exe", base=base)]

packages = ["time", "datetime", "os", "sqlite3", "win32timezone"]

options = {
    'build_exe': {
        'packages':packages,
        'include_files': [
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll')
         ],
    },
}

setup(
    name = "PythonCornerExample",
    options = options,
    description = 'Bla bla',
    executables = executables
)

Running the service using the new executable file now only works if I use the Debug flag and I can't figure out why. When using the Start flag it doesn't do anything, and if I try starting it from Windows Services, I get the "The service did not respond to the start or control request in a timely fashion." error.

I have added the path to the executable to PATH in System Variables, I run cmd as administrator, I have copied pythoncom36.dll from the pywin32_system32 folder to the win32 folder in python's site-packages, I gave permission to users in Registry Editor..

If there is anything you think I missed, please share!

I'm running python3.6 on a Windows7 and I'm using cx_Freeze 5.1.1.

UPDATE:

If this will give you a clue: when running the script (install/start/stop) I get some feedback like "Installing service" or "Starting service". When I run the executable I get no such feedback.

Bibushka
  • 35
  • 9

1 Answers1

0

Please have a look at the cx_Freeze Windows service sample: it looks like you have to use base='Win32Service' as well as a different project structure.

You might still need some additional modifications to make it work with Python 3, see Python 3 executable as windows service.

EDIT: maybe this: Having several issues with a Python service for Windows (built with pyinstaller) is also useful.

jpeg
  • 2,372
  • 4
  • 18
  • 31
  • I have just tried the pyinstaller solution and i get the exact same result: error 1053 when using Start, starts when using Debug. Using `base='Win32Service'` renders the error "The program can't start because cx_Logging.cp36-win_amd64.pyd is missing from your computer. Try reinstalling the program to fix this problem". – Bibushka Jan 14 '19 at 13:02
  • @Bibushka I guess the comment by ZachM to [this answer](https://stackoverflow.com/a/31901728/8516269) might help. – jpeg Jan 14 '19 at 13:28
  • Well, now there's nothing happening. I used the `Win32Service` base with cx_Freeze, the service wouldn't even get installed. I used pyinstaller, it got installed but got error 1053 again when started, but started when using Debug. – Bibushka Jan 14 '19 at 15:43
  • Well, I don't know how I could help further, sorry. Have you looked at this [Microsoft hotfix](https://support.microsoft.com/en-us/help/886695/you-receive-an-error-1053-the-service-did-not-respond-to-the-start-or) and at [this thread](https://stackoverflow.com/q/158371/8516269)? – jpeg Jan 15 '19 at 07:17
  • I have tried pretty much everything that google spits out when searching for windows services built from python code. I have tried logging the steps the executable takes when running in start mode and it stops at this line `hscm = win32service.OpenSCManager(machine,None,win32service.SC_MANAGER_ALL_ACCESS)` in the "win32serviceutil.py" file. I can't go any further into the code because the function's signiture is unknown. Maybe this is a further clue? I'm just at a loss right now, I'm trying everything and anything – Bibushka Jan 15 '19 at 08:50