【问题标题】:Using py2exe to create windows service使用py2exe创建windows服务
【发布时间】:2017-06-20 09:02:58
【问题描述】:

我需要将我的 python 应用程序作为 Windows 服务运行。 我可以使用命令来做到这一点,
python fservice.py install
python fservice.py start

现在,我想使用 py2exe 为我的应用程序创建 exe。 我遵循了这个问题的代码:link

setup.py

from distutils.core import setup
import py2exe
import sys


if len(sys.argv) == 1:
    sys.argv.append("py2exe")
    sys.argv.append("-q")


class Target:
    def __init__(self, **kw):
        self.__dict__.update(kw)
        # for the versioninfo resources
        self.version = "0.0.1"
        self.company_name = "flotomate"
        self.copyright = "no copyright"
        self.name = "flotomate"

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

setup(
     service = [myservice]
    )


fservice.py

import sys

import servicemanager
import win32serviceutil
import win32service
import win32event
import win32api
from pagent import app


class fservice(win32serviceutil.ServiceFramework):
    _svc_name_ = 'flotomate' #here is now the name you would input as an arg for instart
    _svc_display_name_ = 'flotomate' #arg for instart
    _svc_description_ = 'flotomate'# arg from instart

    def __init__(self, *args):
        win32serviceutil.ServiceFramework.__init__(self, *args)
        self.log('init')
        self.stop_event = win32event.CreateEvent(None, 0, 0, None)


        #logs into the system event log
    def log(self, msg):
        import servicemanager
        servicemanager.LogInfoMsg(str(msg))

    def sleep(self, minute):
            win32api.Sleep((minute*1000), True)

    def SvcDoRun(self):
        self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
        try:
            self.ReportServiceStatus(win32service.SERVICE_RUNNING)
            self.log('start')
            self.start()
            self.log('wait')
            win32event.WaitForSingleObject(self.stop_event, win32event.INFINITE)
            self.log('done')
        except Exception:
            self.SvcStop()

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        self.stop()
        win32event.SetEvent(self.stop_event)
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)

    def start(self):
        app.run(host='0.0.0.0',port=4999)

    # to be overridden
    def stop(self):
        pass


if __name__ == '__main__':
    if len(sys.argv) == 1:
        servicemanager.Initialize()
        servicemanager.PrepareToHostSingle(fservice)
        servicemanager.StartServiceCtrlDispatcher()
    else:
        win32serviceutil.HandleCommandLine(fservice)

我正在使用命令创建 exe,
python setup.py py2exe

但是,当我尝试使用
fservice.exe --install

安装服务时 我收到此错误

Traceback (most recent call last):
  File "boot_service.py", line 37, in <module>
AttributeError: 'module' object has no attribute 'Initialize


boot_service.py of py2exe
我正在使用 Python 2.7.6 和 py2exe-0.6.9

【问题讨论】:

    标签: python python-2.7 windows-services py2exe


    【解决方案1】:

    我遇到了同样的问题。不知道你有没有找到解决办法

    在我的情况下,原因是 servicemanager 未包含在编译包中。 python 问题中安装的 servicemanager 库似乎存在冲突。

    为了解决这个问题,如果 servicemanager 未使用,我将其卸载或手动将 servicemanager.pyd 复制到文件夹 dist 和 servicemager.pyc 到dist\library.zip。如果 dist\library.zip 中有名为 servicemanager 的文件夹,删除即可。

    如果你已经有更好的解决方案,请分享^^

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多