Running python scripts using subprocess.call using shebang

I am writing a (somewhat) modular application in Python 3, and I would like to run arbitrary programs from it, with the specified program specified at runtime and not necessarily a python script.

So, I use, for example,

subprocess.call([spam, "-i", eggs, "-o", ham]) 

If spam is a python script, from shebang to python3 and executable permissions, I get

 OSError: [Errno 8] Exec format error 

if I

 subprocess.call(["python3", spam, "-i", eggs, "-o", ham]) 

It works great.

Do you know why? How to run spam without specifying python3 ?

+3
source share
2 answers

You need to use shell=True , and you need your array to be converted to the command line, for example:

 subprocess.call(' '.join([spam, "-i", eggs, "-o", ham]), shell=True) 

This will call the shell instead of the direct command, and the shell should be able to handle shebang.

+8
source

Try

 subprocess.call(['spam.py', "-i", eggs, "-o", ham]) 
-1
source

Source: https://habr.com/ru/post/1484491/


All Articles