小编Nic*_*ole的帖子

如何使用cx_Freeze安装Python Windows服务?

我目前有一个Python文件,当使用python file_name.py运行时会安装一个Windows服务,该服务可以在应用程序日志下的事件查看器中查看,并可以使用sc stop service_name停止.但是,使用cx_Freeze转换为可执行文件时,可执行文件运行时没有错误,但服务不再安装.如果我自己运行可执行文件,如果我运行service_name.exe --install service_name,或者运行sc create service_name binPath = service_path,则会发生这种情况

我的setup.py文件看起来像:

from cx_Freeze import setup, Executable

options = {
'build_exe': {
    'packages': ['packagename'],
    'includes': ['ServiceHandler', 'cx_Logging']}
}

setup(name='cx_FreezeSampleService',
  version='0.1',
  description='Sample cx_Freeze Windows serice',
  executables=Executable('Config.py', base='Win32Service',
           targetName='cx_FreezeSampleService.exe'),
  options=options
  )
Run Code Online (Sandbox Code Playgroud)

我的Config.py看起来像:

NAME = 'cx_FreezeSampleService%s'
DISPLAY_NAME = 'cx_Freeze Sample Service - %s'
MODULE_NAME = 'ServiceHandler'
CLASS_NAME = 'Handler'
DESCRIPTION = 'Sample service description'
AUTO_START = True
SESSION_CHANGES = False
Run Code Online (Sandbox Code Playgroud)

最后,我的ServiceHandler.py看起来像:

class Handler(object):
 def Initialize(self, Config):
    pass

 def Run(self):
    #code to run service …
Run Code Online (Sandbox Code Playgroud)

python windows service cx-freeze

6
推荐指数
1
解决办法
1179
查看次数

标签 统计

cx-freeze ×1

python ×1

service ×1

windows ×1