FileNotFoundError: [Errno 2] No such file or directory: ‘ffmpeg’ when called by launchd

Sympton

FileNotFoundError: [Errno 2] No such file or directory: ‘ffmpeg’ when called by launchd.

It works as expected when called from shell.

Cause

PATH for /usr/local/bin/ffmpeg is missing.

sys.version: (‘3.10.0 (v3.10.0:b494f5935c, Oct 4 2021, 14:59:20) [Clang 12.0.5 ‘
‘(clang-1205.0.22.11)]’)
shutil.which(“python3”): ‘/usr/bin/python3’
os.getenv(“PATH”): ‘/usr/bin:/bin:/usr/sbin:/sbin’
os.getenv(“PYTHONPATH”): None

Solution

Explicitly specify the PATH environment variable in the plist.

shutil.which(“python3”): ‘/usr/local/bin/python3’
os.getenv(“PATH”): ‘/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin’
os.getenv(“PYTHONPATH”): None

Note:

launchctl unload and load is required to update the PATH.
PYTHONPATH is not required.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>test_launchd.test</string>

        <key>EnvironmentVariables</key>
        <dict>
                <key>PATH</key>
                <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
        </dict>

        <key>ProgramArguments</key>
        <array>
                <string>/usr/local/bin/python3</string>
                <string>/users/uchukamen/Desktop/Python/test_launchd/test.py</string>
        </array>

投稿者: admin

Free Software Engineer

コメントを残す

メールアドレスが公開されることはありません。