如何为python安装子进程模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26144305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:09:07  来源:igfitidea点击:

How to install subprocess module for python?

pythonpython-2.7subprocess

提问by avasin

pip is not able to find this module, as well as me on pypi website. Could you please tell me the secret, how to install it?

pip 无法在 pypi 网站上找到这个模块,我也找不到。你能告诉我秘密,如何安装它吗?

I need the module to spawn new shell process via subprocess.call. I have seen a lot of examples, where people use import subprocess, but no one shows how it was installed.

我需要该模块通过 subprocess.call 生成新的 shell 进程。我看过很多例子,人们使用import subprocess,但没有人展示它是如何安装的。

Error, that i got (just in case i've lost my mind and does not understand what is going on):

我得到的错误(以防万一我失去理智并且不明白发生了什么):

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

采纳答案by David Heffernan

There is no need to install this module in Python 2.7. It is a standard module that is built in.

在 Python 2.7 中无需安装此模块。它是一个内置的标准模块。

The documentationshows that it was added to the library for Python version 2.4. It's been with us for a long time now.

文件表明,它加入了Python版本2.4库。它已经伴随我们很长时间了。



The error that you show in your question update is nothing more prosaic than a file not found error. Likely the executable file that you are attempting to call Popenon cannot be found.

您在问题更新中显示的错误只不过是找不到文件错误更平淡无奇。可能Popen找不到您尝试调用的可执行文件。

That traceback indicates that subprocessis installed and has been imported. The problem is simply that the call to subprocess.call('py.test')is failing.

该回溯表明subprocess已安装并已导入。问题只是调用subprocess.call('py.test')失败。



For future reference, this is the type of traceback you encounter when attempting to import a module that has not been installed:

为了将来参考,这是您在尝试导入尚未安装的模块时遇到的回溯类型:

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

回答by Don Question

The error-text is misleading. Most subprocess-commands expect the shellcmd to be submitted as a list of strings.

错误文本具有误导性。大多数子进程命令期望 shellcmd 作为字符串列表提交。

In these cases i strongly recommend the usage of the shlex module:

在这些情况下,我强烈建议使用 shlex 模块:

import shlex

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

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

or in this simple case just:

或者在这个简单的情况下:

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