Skip to main content
 首页 » 编程设计

python之使用 pyinstaller 问题编译 Windows 服务

2025年05月04日11haluo1

我已经实现了一个 Windows 服务,这个服务在使用 pyinstaller 编译之前没有问题,但是在服务启动命令之后它给出了 1053 错误。

Windows 服务代码:

import sys 
import win32service 
import win32event 
import socket 
import win32api 
import win32serviceutil 
 
 
class AppServerSvc(win32serviceutil.ServiceFramework): 
    _svc_name_ = "test" 
    _svc_display_name_ = "test" 
    _stoped = False 
 
    def __init__(self, *args): 
        win32serviceutil.ServiceFramework.__init__(self, *args) 
        self.stop_event = win32event.CreateEvent(None, 0, 0, None) 
 
    def SvcStop(self): 
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) 
        win32event.SetEvent(self.hWaitStop) 
        self._stoped = True 
 
    def SvcDoRun(self): 
        self.ReportServiceStatus(win32service.SERVICE_RUNNING) 
        self.main() 
 
    def main(self): 
        while True: 
            if self._stoped: 
                break 
            pass 
 
if __name__ == '__main__': 
    win32serviceutil.HandleCommandLine(AppServerSvc) 

请您参考如下方法:

最后我解决了这个问题,Windows 服务中的一个引用链接断开了。这是 py2exe 的正确配置,它解决了我的问题:

opts = {'py2exe': { 
'dll_excludes': ['libzmq.pyd', 'OLEAUT32.dll', 'USER32.dll', 'SHELL32.dll', 'ole32.dll', 
                 'MSVCP90.dll', 'ADVAPI32.dll', 'NETAPI32.dll', 'WS2_32.dll', 'GDI32.dll', 
                 'VERSION.dll', 'KERNEL32.dll', 'WINSPOOL.DRV', 'mfc90.dll', 'ntdll.dll'], 
'includes': ['UserList', 'UserString', 'commands', 'zmq.backend.cython'], 
'dist_dir': "dist" 
}} 
 
setup(service=[service], options=opts, zipfile=None,data_files=[(os.path.join(os.getcwd(), 'dist'), (zmq.libzmq.__file__,))])