Title: Subprocess does not find executable on Windows if it is PATH with quotes
Type: behavior Stage: resolved
Components: Library (Lib), Windows Versions: Python 3.4, Python 3.5, Python 2.7
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: tim.golden Nosy List: BreamoreBoy, Eli_B, pekka.klarck, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2013-01-24 13:09 by pekka.klarck, last changed 2015-02-08 12:53 by tim.golden. This issue is now closed.

Messages (3)
msg180520 - (view) Author: Pekka Klärck (pekka.klarck) Date: 2013-01-24 13:09
If you add a directory into PATH on Windows so that the directory is in quotes, subprocess does not find executables in it. They are found by the operating system, though, at least when run on the command prompt.

To reproduce:

C:\>python --version
Python 2.7.3
C:\>type test\script.bat
@echo off
echo hello
C:\>set ORIG=%PATH%
C:\>set PATH=%ORIG%;test
C:\>python -c "from subprocess import call; call('script.bat')"
C:\>set PATH=%ORIG%;"test"
C:\>python -c "from subprocess import call; call('script.bat')"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python27\lib\", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\Python27\lib\", line 679, in __init__
    errread, errwrite)
  File "C:\Python27\lib\", line 896, in _execute_child
WindowsError: [Error 2] The system cannot find the file specified

I don't think you ever need those quotes, even if the directory would have spaces, but it is very confusing that the behavior is different on the command prompt and with subprocess. Additionally, Linux does not suffer from this problem:

$ python --version
Python 2.7.3
$ cat test/ 
echo "hello"
$ PATH=$PATH:test
$ PATH=$PATH:test python -c "from subprocess import call; call('')"
$ PATH=$PATH:"test"
$ PATH=$PATH:"test" python -c "from subprocess import call; call('')"
msg222891 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-12 22:29
@Pekka sorry about the delay in getting back to you.

I can confirm that this is still in issue in 3.4.1 and 3.5.0a0.
msg235393 - (view) Author: Tim Golden (tim.golden) * (Python committer) Date: 2015-02-04 18:06
Under the covers, subprocess is calling CreateProcess so there's really not very much we can do here, short of writing our own PATH-handling.

As a matter of fact, passing shell=True will produce the desired effect. Since the only thing this does is to run the process under cmd.exe I assume that cmd.exe itself adds some kind of PATH handling of its own.

I recommend closing as "won't fix".
Date User Action Args
2015-02-08 12:53:07tim.goldensetstatus: open -> closed
resolution: wont fix
stage: resolved
2015-02-04 18:06:13tim.goldensetnosy: + zach.ware, tim.golden, steve.dower
messages: + msg235393

assignee: tim.golden
components: + Windows
2015-02-04 17:50:24Eli_Bsetnosy: + Eli_B
2014-07-12 22:29:41BreamoreBoysetversions: + Python 2.7, Python 3.4, Python 3.5
nosy: + BreamoreBoy

messages: + msg222891

components: + Library (Lib)
type: behavior
2013-01-24 13:09:21pekka.klarckcreate