Executing python scripts with subprocess.call using shebang

I'm writing a (somewhat) modular application in Python 3 and I'd like to run arbitrary programs from it, said program being 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, with shebang to python3 and executable rights, I get

OSError: [Errno 8] Exec format error

if I

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

it works fine.

Do you know why? How can I run spam without specifying python3?


You need to use shell=True, and you need your array to be turned into a command string, like this:

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

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


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

Need Your Help

Magento API: How can I assign an image to multiple products without re-uploading

magento magento-1.5 magento-1.6

The sites I work on (Magento 1.5 and 1.6) currently use a script that manually places images and then associates them using MySQL queries. I'm tasked with trying to move to an API-centric method s...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.