2014-10-01 25 views
8

pip無法在pypi網站上找到此模塊以及我。 你能告訴我祕密,如何安裝它?如何爲python安裝子進程模塊?

我需要模塊通過subprocess.call產生新的shell進程。我見過很多例子,其中人們使用import subprocess,但沒有人展示它是如何安裝的。

錯誤,我得到了(以防萬一我失去了我的頭腦,不明白是怎麼回事):

Microsoft Windows [Version 6.3.9600] 
(c) 2013 Microsoft Corporation. All rights reserved. 

C:\Users\Alexander\Desktop\tests-runner>python run.py 
Traceback (most recent call last): 
    File "run.py", line 165, in <module> 
    main() 
    File "run.py", line 27, in main 
    subprocess.call('py.test') 
    File "C:\Python27\lib\subprocess.py", line 522, in call 
    return Popen(*popenargs, **kwargs).wait() 
    File "C:\Python27\lib\subprocess.py", line 710, in __init__ 
    errread, errwrite) 
    File "C:\Python27\lib\subprocess.py", line 958, in _execute_child 
    startupinfo) 
WindowsError: [Error 2] The system cannot find the file specified 
+3

它的內置,您不必安裝它。你的Python版本是什麼? – 2014-10-01 14:57:48

+1

'subprocess'模塊是標準庫的一部分,它不需要(不能,實際上)安裝。您得到的錯誤表明應該作爲子進程運行的* executable *無法找到,而不是'subprocess'模塊本身。可能是工作目錄「$ PATH」的問題,或者可執行文件不是'.exe'。 – 2014-10-01 15:04:11

+0

什麼類型的文件是'py.test'? Windows不知道它應該如何執行擴展名爲「.test」的文件,所以你至少需要指定一個解釋器來執行它。看到[這個問題](http://stackoverflow.com/questions/4965175/make-subprocess-find-git-executable-on-windows/10555130)爲'git.cmd'這個問題的例子。 – 2014-10-01 15:07:34

回答

7

沒有必要在Python 2.7安裝此模塊。它是內置的標準模塊。

documentation表明它已添加到Python版本2.4的庫中。我們已經有很長一段時間了。


您在問題更新中顯示的錯誤沒有比找不到文件錯誤更平淡無奇。可能找不到您嘗試撥打Popen的可執行文件。

回溯表示subprocess已安裝且已導入。問題在於撥打subprocess.call('py.test')失敗。


以供將來參考,這是試圖導入尚未安裝模塊,當你遇到回溯類型:

 
>>> import foo 
Traceback (most recent call last): 
    File "", line 1, in 
ImportError: No module named foo 
+0

我剛剛更新了問題並添加了錯誤文本..也許我沒有正確理解它? – avasin 2014-10-01 15:01:42

+0

是的,你是完全正確的。辛苦的一天...將在8分鐘內接受答案。 – avasin 2014-10-01 15:02:47

2

錯誤文本是一種誤導。大多數子處理命令都希望將shellcmd作爲字符串列表提交。

在這種情況下,我強烈建議shlex模塊的使用:

import shlex 

shell_cmd = "py.test" # or should it be "test.py" ? 

subprocess_cmd = shlex.split(shell_cmd) 
subprocess.call(subprocess_cmd) 

或在這個簡單的例子只是:

subprocess.call(["py.test"])