subprocess.Popen(): OSError: [Errno 8] Exec format error in python?

Solution 1:

I solved this by putting this line at the top of the called shell script:

#!/bin/sh

That will guarantee that the system always uses the correct interpreter when running your script.

Solution 2:

Following statement worked for me

subprocess.Popen(['sh','my_script.sh'])

Solution 3:

The error message suggests that the external program is not a valid executable.

Solution 4:

As @tripleee said, there is an issue executing your script. Make sure:

  • Change the shell command to "./my_path/my_script.sh" or "/bin/bash my_path/my_script.sh". Account for environment variables, if necessary.
  • Both scripts have execute bit set (chmod +x)
  • The files exist at the location you think they do. (Use abspath or verify environment)
  • The files have contents
  • Try removing and re-typing the first line. I recommend killing the whole line, and hitting backspace several times in case there's a non-printable character before the #!

See also: Why is '#!/usr/bin/env python' supposedly more correct than just '#!/usr/bin/python'?