10

Details error message:-

 FileNotFoundError

  [WinError 2] The system cannot find the file specified

  at ~\AppData\Local\Programs\Python\Python39\lib\subprocess.py:1582 in _execute_child
      1578│             sys.audit("subprocess.Popen", executable, args, cwd, env)
      1579│ 
      1580│             # Start the process
      1581│             try:
    → 1582│                 hp, ht, pid, tid = _winapi.CreateProcess(
      1583│                     executable,
      1584│                     args,
      1585│                     # no special security
      1586│                     None,
make: *** [makefile:14: format] Error 1

We have something similar issue listed here: https://bugs.python.org/issue17023

The file is there, the path is fine too. But why am I getting this error as the file is there at the specified location?

I`m getting this error while running formatter linters.

4 Answers 4

11

You can just set shell = True and pass it to the subprocess class you are using. Modifying the library file will cause compatibility problems later on with codes from other programmers. To get some insight into why we need to set this variable, take a look at the documentation: "args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments."

0

Example which produce error in my env.

import concurrent.futures
import multiprocessing
import random
import subprocess


def worker(workerid):
    print(f"start {workerid}")
    p = subprocess.Popen(["sleep", f"{random.randint(1,30)}"])
    p.wait()
    print(f"stop {workerid}")
    return workerid


def main():
    tasks = []
    with concurrent.futures.ThreadPoolExecutor(max_workers=20) as pool:
        for i in range(20):
            tasks.append(pool.submit(worker, i))

        print("waiting for tasks...", flush=True)
        for task in concurrent.futures.as_completed(tasks):
            print(f"completed {task.result()}", flush=True)
        print("done.")


if __name__ == "__main__":
    main()
2
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jan 24 at 12:57
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review
    – hlongmore
    Jan 29 at 7:10
0

I faced with this kind of error and I realize that if I change windows path to posix-style with pathlib.PureWindowsPath(<path>).as_posix(), it is working. Here what I do:

import subprocess as sp
import pathlib
import shlex

exe_path = r"C:\ffmpeg\bin\ffmpeg.exe"
print(f"exe path: {exe_path}")

try:
    cmd = f"{exe_path} -version"
    sp.Popen(shlex.split(cmd))
    sp.wait()
except FileNotFoundError as e:
    print(e)
    print("\n")

exe_path = pathlib.PureWindowsPath(exe_path).as_posix()
print(f"exe path: {exe_path}")

cmd = f"{exe_path} -version"
t=sp.Popen(shlex.split(cmd))
t.wait()

Out-->:

C:\Users\Veysel\Desktop>python file.py
exe path: C:\ffmpeg\bin\ffmpeg.exe
[WinError 2] The system cannot find the file specified

exe path: C:/ffmpeg/bin/ffmpeg.exe
ffmpeg version 2022-10-17-git-3bd0bf76fb-essentials_build-www.gyan.dev Copyright (c) 2000-2022 the FFmpeg developers
-3

IMPORTANT NOTE:- In case of any modification made to the library file, it might cause compatibility issues later on with codes for other programmers. To get more information, about the necessity to set value for the shell, plz refer to the official docs, proper link is provide here.

As this error caused trouble in my work I did the following solution for now and everything is working fine for me.

To solve this error:- We have to modify the subprocess.py in your environment.

First of all, you have to locate this file and then edit it. In my PC it location is - C:\Users\User\AppData\Local\Programs\Python\Python39\Lib.

In this piece of code:-

def __init__(self, args, bufsize=-1, executable=None,
             stdin=None, stdout=None, stderr=None,
             preexec_fn=None, close_fds=_PLATFORM_DEFAULT_CLOSE_FDS,
             shell=True, cwd=None, env=None, universal_newlines=False,
             startupinfo=None, creationflags=0,
             restore_signals=True, start_new_session=False,
             pass_fds=(), *, encoding=None, errors=None):

You have to change the value of shell.
Change shell=False to shell = True.

This solution worked for me, I hope it`ll work for you too.

Thank you.

4
  • 2
    This is a very very very bad idea. Do not mess with core Python libraries that have been installed on your system in order to avoid fixing bugs in your own application. Oct 6, 2022 at 15:29
  • 1
    Alright. Thank you for the feedback. Kindly provide a good solution to solve this error. Oct 7, 2022 at 3:25
  • It's impossible to answer this question without seeing your actual code. Please read: stackoverflow.com/help/minimal-reproducible-example Oct 7, 2022 at 12:42
  • Note also that the Python bug you linked was closed because it's not considered a bug, and refers only to a specific situation. Without seeing your code, it's impossible to know if your situation is the same. Oct 7, 2022 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.