2014-10-01 73 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"])