Windows can't find the file on subprocess.call()

PythonPathPython 3.x

Python Problem Overview


I am getting the following error:

WindowsError: [Error 2] The system cannot find the file specified

My code is:

subprocess.call(["<<executable file found in PATH>>"])

Windows 7, 64 bit. Python 3.x latest, stable.

Any ideas?

Thanks,

Python Solutions


Solution 1 - Python

When the command is a shell built-in, add a shell=True to the call.

E.g. for dir you would type:

import subprocess
subprocess.call('dir', shell=True)

To quote from the documentation:

> The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need shell=True to run a batch file or console-based executable.

Solution 2 - Python

On Windows, I believe the subprocess module doesn't look in the PATH unless you pass shell=True because it use CreateProcess() behind the scenes. However, shell=True can be a security risk if you're passing arguments that may come from outside your program. To make subprocess nonetheless able to find the correct executable, you can use shutil.which. Suppose the executable in your PATH is named frob:

subprocess.call([shutil.which('frob'), arg1, arg2])

(This works on Python 3.3 and above.)

Solution 3 - Python

On Windows you have to call through cmd.exe. As Apalala mentioned, Windows commands are implemented in cmd.exe not as separate executables.

e.g.

subprocess.call(['cmd', '/c', 'dir'])

/c tells cmd to run the follow command

This is safer than using shell=True, which allows shell injections.

Solution 4 - Python

If you are using powershell, then in it will be subprocess.call(['powershell','-command','dir']). Powershell supports a large portion of POSIX commands

Solution 5 - Python

After much head scratching, I discovered that running a file that is located in C:\Windows\System32\ while running a 32bit version of python on a 64bit machine is a potential issue, due to Windows trying to out-smart the process, and redirect calls to C:\Windows\System32 to C:\Windows\SysWOW64.

I found an example of how to fix this here: http://code.activestate.com/recipes/578035-disable-file-system-redirector/

Solution 6 - Python

To quote from the documentation:

"Prior to Python 3.5, these three functions comprised the high level API to subprocess. You can now use run() in many cases, but lots of existing code calls these functions."

SO: instead of subprocess.call use subprocess.run for Python 3.5 and above

Solution 7 - Python

I met the same issue while I was calling a PHP. The reason is PHP isn't in PATH so the command PHP was not found. But PowerShell found it does exist in the current location and it suggests replacing the 'PHP' by the '.\PHP' if I trust this command. Then it runs well.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSriView Question on Stackoverflow
Solution 1 - PythonDouglas MacdonaldView Answer on Stackoverflow
Solution 2 - PythonptomatoView Answer on Stackoverflow
Solution 3 - PythonSam InversoView Answer on Stackoverflow
Solution 4 - PythonAlexander NozikView Answer on Stackoverflow
Solution 5 - PythonRBNView Answer on Stackoverflow
Solution 6 - PythonAdrian BercaView Answer on Stackoverflow
Solution 7 - Pythonmichael xieView Answer on Stackoverflow