启动用 Python 编写的窗口服务时出现错误 1053

启动用 Python 编写的窗口服务时出现错误 1053

问题描述:

我已经查看并尝试了其他人发布的这个问题的解决方案.一位用户说要尝试从以下位置更改我的 setup.py 文件:

I have already looked at and tried the resolutions to this question that others have posted. One user said that to try and change my setup.py file from:

from distutils.core import setup
import py2exe

setup(console=["dev.py"])

from distutils.core import setup
import py2exe

setup(service=["dev.py"])

我得到了以下结果:

running py2exe
*** searching for required modules ***
Traceback (most recent call last):
  File "C:\Python27\Scripts\distutils-setup.py", line 5, in <module>
  setup(service=["C:\Python27\Scripts\dev.py"])
File "C:\Python27\lib\distutils\core.py", line 152, in setup
  dist.run_commands()
File "C:\Python27\lib\distutils\dist.py", line 953, in run_commands
  self.run_command(cmd)
File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
  cmd_obj.run()
File "C:\Python27\lib\site-packages\py2exe\build_exe.py", line 243, in run
  self._run()
File "C:\Python27\lib\site-packages\py2exe\build_exe.py", line 296, in _run
  self.find_needed_modules(mf, required_files, required_modules)
File "C:\Python27\lib\site-packages\py2exe\build_exe.py", line 1274, in
find_needed_modules
  mf.import_hook(mod)
File "C:\Python27\lib\site-packages\py2exe\mf.py", line 719, in import_hook
  return Base.import_hook(self,name,caller,fromlist,level)
File "C:\Python27\lib\site-packages\py2exe\mf.py", line 136, in import_hook
  q, tail = self.find_head_package(parent, name)
File "C:\Python27\lib\site-packages\py2exe\mf.py", line 204, in find_head_package
  raise ImportError, "No module named " + qname
ImportError: No module named dev

现在,当我在安装脚本中使用控制台"运行 py2exe 时,它​​运行良好,但该服务无法启动,并且出现错误.当我在安装脚本中使用service"运行 py2exe 时,py2exe 不会运行并告诉我它找不到我的模块.

Now, when I run py2exe with "console" in my setup script it works fine, but the service doesn't start and I get the error. When I run py2exe with "service" in my setup script py2exe doesn't run and tells me it can't find my module.

我尝试重新安装 py2exe 没有解决.我也尝试过改变:

I have tried to re-install py2exe to no resolution. I have also tried to change:

def SvcDoRun(self):
    servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                          servicemanager.PYS_SERVICE_STARTED,
                          (self._svc_name_,''))

def  SvcDoRun(self):
     self.ReportServiceStatus(win32service.SERVICE_RUNNING)
     win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

也没啥区别.任何人都可以帮助我吗?这是我正在做的事情.它监视服务器并每 60 秒返回一个文本文件,我用它在任何给定分钟监视我的服务器.你们能提供的任何帮助都会很棒.

Didn't make a difference either. CAN ANYONE HELP ME PLEASE? Here is what I am working on. It monitors a server and spits back a text file every 60 seconds which I use to monitor my servers at any given minute. Any help you guys and gals can give would be great.

import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import wmi
import _winreg
from time import sleep
import os

class SrvMonSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "SrvMonSvc"
_svc_display_name_ = "Server Monitor"

def __init__(self,args):
    win32serviceutil.ServiceFramework.__init__(self,args)
    self.hWaitStop = win32event.CreateEvent(None,0,0,None)
    socket.setdefaulttimeout(60)

def SvcStop(self):
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    win32event.SetEvent(self.hWaitStop)

def SvcDoRun(self):
    servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                          servicemanager.PYS_SERVICE_STARTED,
                          (self._svc_name_,''))
    self.main()

def main(self):
    host = wmi.WMI(namespace="root/default").StdRegProv
    try:
        result, api = host.GetStringValue(
        hDefKey = _winreg.HKEY_LOCAL_MACHINE,
        sSubKeyName = "SOFTWARE\Server Monitor",
        sValueName = "API")
        if api == None:
            raise Exception
        else:
            pass
    except:
        exit()

    while 1 == 1:
        with open("C:/test.txt", "wb") as b:
            computer = wmi.WMI(computer="exsan100")
            for disk in computer.Win32_LogicalDisk (DriveType=3):
                name = disk.caption
                size = round(float(disk.Size)/1073741824, 2)
                free = round(float(disk.FreeSpace)/1073741824, 2)
                used = round(float(size), 2) - round(float(free), 2)
                for mem in computer.Win32_OperatingSystem():
                    a_mem = (int(mem.FreePhysicalMemory)/1024)
                for me in computer.Win32_ComputerSystem():
                    t_mem = (int(me.TotalPhysicalMemory)/1048576)
                    u_mem = t_mem - a_mem
                for cpu in computer.Win32_Processor():
                    load = cpu.LoadPercentage
                print >>b, api
                print >>b, name
                print >>b, size
                print >>b, used
                print >>b, t_mem
                print >>b, u_mem
                print >>b, load
        b.close()
        date_list = []
        stamp = time.strftime("%c",time.localtime(time.time()))
        date_list.append(stamp)
        name = re.sub(r"[^\w\s]", "",date_list[0])
        os.rename("C:/test.txt", ("C:/%s.txt" % name))

        try:
            sleep(60.00)
        except:
            exit()

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(SrvMonSvc)

您是否从最初的问题中取得了进展.我在 python 服务上遇到了类似的问题,发现它缺少 DLL,因为系统路径"(不是用户路径)不完整.

Have you progressed from your original problem. I had similar problem with a python service and found out that it was missing DLLs since the 'System Path' (not the user path) was not complete.

从命令提示符运行带有 -debug 的 pythonservice.exe 不是问题,因为它使用了正确的 PATH 环境变量,但如果您的服务安装为系统服务,则值得检查系统路径变量是否包含所有路径所需的 DLL(MSVC、Python、System32).对我来说,它缺少 python DLL 路径,之后它又工作了.

Running pythonservice.exe with -debug from the command prompt was not a problem because it used correct PATH environment variable, but if your service is installed as a System service it's worth checking out if the System Path variable has all the paths for the required DLLs (MSVC, Python, System32). For me it was missing the python DLLs path, after that it worked again.