18

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?

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.

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
avasin
  • 9,186
  • 18
  • 80
  • 127
  • 4
    it's builtin, you don't have to install it. what is your python version? – Rafael Barros Oct 01 '14 at 14:57
  • 1
    The `subprocess` module is part of the standard library, it doesn't need (can't, actually) be installed. The error you get indicates that the *executable* that is supposed to be run as a subprocess can't be found, not the `subprocess` module itself. Might be an issue with the working directory, `$PATH`, or the executable not being an `.exe`. – Lukas Graf Oct 01 '14 at 15:04
  • 1
    What type of file is `py.test`? Windows doesn't know how it should execute a file with a `.test` extension, so you'd need to at least specify an interpreter to execute it with. See [this question](http://stackoverflow.com/questions/4965175/make-subprocess-find-git-executable-on-windows/10555130) for an example of this problem with `git.cmd`. – Lukas Graf Oct 01 '14 at 15:07
  • try `py.test.exe` name. Windows might fail to add `.exe` suffix because it thinks that the file already has it (`.test`). Here's [how Windows search for the executable file](http://stackoverflow.com/a/25167402/4279) – jfs Oct 04 '14 at 13:18
  • One update: I see this error only if I execute in IDEL (Python GUI) or even directly in Windows dos shell. If I execute it in GIT shell as commandline it works fine. – Sam Apr 01 '17 at 15:11

2 Answers2

16

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

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


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 Popen on cannot be found.

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


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
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

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

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

    import shlex
    
    shell_cmd = "test.py"
    
    subprocess_cmd = shlex.split(shell_cmd)
    subprocess.call(subprocess_cmd)

or in this simple case just:

    subprocess.call(["test.py"])
Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
Don Question
  • 11,227
  • 5
  • 36
  • 54