2012-05-11 127 views
0

我已經看過並嘗試過其他人發佈的這個問題的解決方案。一位用戶說,嘗試從改變我的setup.py文件:錯誤1053當啓動窗口服務在Python中寫入

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不能運行,並告訴我它找不到我的模塊。

我試圖重新安裝py2exe至無法解析。我也試圖改變:

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秒回傳一個文本文件,用於在任何給定的時間監控我的服務器。任何幫助你們和加爾可以給予的幫助將會很好。

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) 

回答

1

你是否從原來的問題發展而來。我有一個與python服務類似的問題,並發現它缺少DLL,因爲'系統路徑'(不是用戶路徑)沒有完成。

使用-debug從命令提示符運行pythonservice.exe並不是問題,因爲它使用了正確的PATH環境變量,但是如果您的服務作爲系統服務安裝,那麼值得檢查一下系統路徑變量是否包含所有路徑爲所需的DLL(MSVC,Python,System32)。對我來說,它錯過了python DLLs的路徑,然後再次運行。

相關問題