OSError: [Errno 8] Exec format error in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Table of Contents

  1. OSError: [Errno 8] Exec format error in Python
  2. Make sure the script you are trying to run is an executable file
  3. Solving the error by using the subprocess.call() method
  4. Make sure the file is not corrupted

# OSError: [Errno 8] Exec format error in Python [Solved]

The Python "OSError: [Errno 8] Exec format error" occurs for 3 main reasons:

  1. Trying to run a shell script with a missing or incorrect shebang.
  2. Not having the necessary executable permissions to run the shell script.
  3. The shell script file having an incorrect type, being incomplete or corrupted (e.g. because a download was interrupted).

Let's look at an example of how the error occurs.

This is the shell script that is stored in a file named my-script.sh.

my-script.sh
echo bobbyhadz.com echo google.com

First, make sure you have the necessary permissions to run the script if you are on macOS or Linux.

Open your terminal in the same directory as the my-script.sh file and issue the following command.

shell
chmod +x my-script.sh

make-shell-script-executable

The command will give you the necessary permissions to execute the shell script.

And here is the main.py file that tries to run the script.

main.py
import subprocess path_to_script = './my-script.sh' # ⛔ OSError: [Errno 8] Exec format error: './my-script.sh' subprocess.run(path_to_script, check=False)

os error errno 8 exec format error

The error is caused because our shell script doesn't have a shebang.

You have to add #!/bin/sh as the first line in your shell script.

my-script.sh
#!/bin/sh echo bobbyhadz.com echo google.com

The #!/bin/sh characters are also known as a shebang.

The shebang character sequence starts with a hash # and an exclamation mark !.

The shebang is used to instruct the Python script to run the shell script in /bin/sh.

If you use bash as your shell, you can also set the shebang character sequence to #!/bin/bash.

my-script.sh
#!/bin/bash echo bobbyhadz.com echo google.com

However, the #!/bin/sh character sequence is more widely used.

Having updated my shell script to the following:

my-script.sh
#!/bin/sh echo bobbyhadz.com echo google.com

I'll now run the Python script with python main.py.

error solved after setting shebang character sequence

Everything works as expected now that the shebang character sequence has been added to the top of the shell script.

Note: If you need to run a Python script, then set the shebang to #!/usr/bin/env python or #!/usr/bin/env python3.

my-script.py
#!/usr/bin/env python

Or:

my-script.py
#!/usr/bin/env python3

Depending on your Python version, you might have to use python or python3.

You also have to make sure the file is executable.

shell
chmod +x my-script.py

# Make sure the script you are trying to run is an executable file

Another thing you should make sure is that the script you're trying to run is an executable file.

You can use the chmod command to enable the current user to execute the shell script.

shell
chmod +x my-script.sh

make-shell-script-executable

However, you need to also make sure that the file is an actual executable script and not just any other file.

On macOS and Linux, you can use the file command to check if the file is executable.

shell
file my-script.sh

check if file is executable

Make sure to replace the my-script.sh placeholder with the actual name of your script.

If you get a message similar to the following: "my-script.sh: Bourne-Again shell script, ASCII text executable", then the file is executable.

# Solving the error by using the subprocess.call() method

You can also solve the error by passing sh as the first element in the array that is passed to the subprocess.call() method.

Suppose you have the following shell script.

my-script.sh
echo bobbyhadz.com echo google.com

You can run the shell script as follows.

main.py
import subprocess path_to_script = './my-script.sh' # bobbyhadz.com # google.com subprocess.call(['sh', path_to_script])

solve error by using sh first list item

The shell script didn't contain a shebang (#!/bin/bash) but we still didn't get the error.

This is because we passed sh as the first item of the list we passed to subprocess.call().

Python already knows that the script should be run using /bin/sh.

The same can be achieved when using the subprocess.Popen method.

main.py
import subprocess path_to_script = './my-script.sh' # bobbyhadz.com # google.com subprocess.Popen(['sh', path_to_script])

solve the error when using subprocess popen

# Make sure the file is not corrupted

If the error persists, make sure that the file is not corrupted.

You can try to run the shell script directory from your terminal.

shell
./my-script.sh

make sure file is not corrupted

The file might be corrupted if you tried to download the script from somewhere but the download or file transfer was interrupted.

The file should also be executable for you to be able to run it from a Python script.

# Conclusion

To solve the Python "OSError: [Errno 8] Exec format error", make sure:

  1. Your shell script contains a shebang #!/bin/sh character sequence at the start.
  2. You have the necessary executable permissions to run the shell script.
  3. The shell script file is not corrupted.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.