FileNotFoundError in subprocess: Command Not Found

FileNotFoundError: [Errno 2] No such file or directory: 'my-script'

Quick Answer

The command you are running with subprocess does not exist or is not on the PATH. Verify installation, use full path, or check with shutil.which().

Why This Happens

subprocess.run() looks for the executable on the system PATH. If not installed or misspelled, it raises FileNotFoundError. On Linux and macOS the same error appears when a command with arguments is passed as one string without shell=True: subprocess.run('ls -l') makes Python look for an executable literally named 'ls -l'.

The Problem

import subprocess
result = subprocess.run(['my-script', '--flag'])

The Fix

import subprocess, shutil
if shutil.which('my-script'):
    result = subprocess.run(['my-script', '--flag'])
else:
    print('my-script is not installed')

Step-by-Step Fix

  1. 1

    Verify command exists

    Use shutil.which('command') to check.

  2. 2

    Use full path

    Specify the complete path to the executable.

  3. 3

    Pass a list, not a string

    Split the command into a list such as ['ls', '-l'] (shlex.split() helps), or the whole string is treated as the program name.

  4. 4

    Check shell features

    Use shell=True for pipes/redirects, but avoid with untrusted input.

Got the actual stack trace?

Paste it into our free AI explainer to get the cause and the fix for your specific case — no signup, nothing to install.

Explain my error

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free