2010-06-09 199 views
26

我正在使用killableprocess包(建立在子進程之上)運行進程 每當我在腳本中運行「killableprocess.Popen(command)」代碼段時,以下錯誤:WindowsError [錯誤5]訪問被拒絕

File "killableprocess.py", line 157, in _execute_child 
    winprocess.AssignProcessToJobObject(self._job, hp) 
File "winprocess.py", line 37, in ErrCheckBool 
    raise WinError() 
WindowsError [error 5] Access is denied 
Exception TypeError: "'NoneType' object is not callable" in <bound method AutoHANDLE.__del__ of <AutoHANDLE object at 0x025D42B0>> ignored 

但是,當我從python交互式控制檯(python 2.6)運行它時,它工作正常。 這可能意味着當我從腳本運行該腳本時存在權限問題,但我不知道如何解決它們。我嘗試從以管理員身份運行的cmd運行腳本,但沒有幫助。 試圖尋找類似的帖子,但沒有找到任何好的解決方案。希望在這裏找到一些幫助 我在Windows上運行,特別是Windows 7旗艦版x64,如果它有任何幫助。

感謝

回答

2

另外,如果你的模塊不能正常工作,您可以使用«子»模塊:

import subprocess, os, time 

process = subprocess.Popen("somecommand", shell=True) 
n = 0 
while True: 
    if not process.poll(): 
     print('The command is running...') 
     if n >= 10: 
      pid = process.pid() 
      os.kill(pid, 9) # 9 = SIGKILL 
    else: 
     print('The command is not running..') 
    n += 1 
    time.sleep(1) 
+0

取出括號中的'process.pid()'(「類型錯誤:‘詮釋’對象不是可調用「) – 2011-06-08 17:42:50

0

你指定完整路徑爲可執行要傳遞到Popen(第一項在argv)?

9

我解決了類似的問題,我通過切換到進程目錄(我試圖用Inkscape中),它解決了我的問題

import subprocess 
inkscape_dir=r"C:\Program Files (x86)\Inkscape" 
assert os.path.isdir(inkscape_dir) 
os.chdir(inkscape_dir) 
subprocess.Popen(['inkscape.exe',"-f",fname,"-e",fname_png]) 

也許切換到進程目錄會爲你工作了。

8

當我用子進程模塊運行時發現,我發現'args'中的第一個條目(subprocess.Popen()的第一個參數)只是沒有路徑的可執行文件名,我需要在參數中設置executable列表到我的可執行文件的完整路徑。

app = 'app.exe' 
appPath = os.path.join(BIN_DIR, app) 

commandLine = [app, 'arg1', 'arg2'] 
process = subprocess.Popen(commandLine, executable=appPath) 
+0

也要注意當前的工作目錄;其他答案建議在啓動過程之前需要'os.chdir(other_dir)',這可能是真實的,具體取決於過程本身的實現。但是,您也可以使用'cwd = other_dir'參數來指定'Popen'來設置cwd,而無需爲腳本本身進行更改。 – 2017-07-05 16:13:21

3

請確保您的路徑包括可執行文件的名稱(inkscape.exe)

+0

很難趕上這一個! – mireazma 2017-12-30 13:38:56

相關問題