Mehdi Khlifi
Mehdi Khlifi

Reputation: 415

Running command with subprocess raises FileNotFoundError

I'm trying to run the following commands in a Python script

import subprocess
image_name = "alpine:3.10"
scan_image = "trivy -q image -f json {}".format(image_name)
scan_result = subprocess.check_output(scan_image.split()).decode('utf-8')

If I run it from a Python script, it raises the following error: [Errno 2] No such file or directory: 'trivy'

But if I run the command using python interpreter (interactive mode) it works fine.

-bash-4.2# python3
Python 3.6.8 (default, Apr  2 2020, 13:34:55) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> image_name = "alpine:3.10"
>>> scan_image = "trivy -q image -f json {}".format(image_name)
>>> subprocess.check_output(scan_image.split()).decode('utf-8')
'[\n  {\n    "Target": "alpine:3.10 (alpine 3.10.5)",\n    "Type": "alpine",\n    "Vulnerabilities": null\n  }\n]'
>>> 

What could I be doing wrong? I've run the commands using Python2 and Python3 interpreters.

Upvotes: 0

Views: 229

Answers (1)

JacobIRR
JacobIRR

Reputation: 8966

Security concerns aside, you need to provide the full path to your executable:

Replace trivy in the script with the results of which trivy from a shell

Upvotes: 1

Related Questions