How to Fix OSError: [Errno 8] Exec Format Error in Python

Rohan Timalsina Feb 02, 2024
  1. Recreate OSError: [Errno 8] Exec format error in Linux
  2. Add #!/bin/sh to Fix OSError: [Errno 8] Exec format error in Linux
  3. Use sh to Fix OSError: [Errno 8] Exec format error in Linux
How to Fix OSError: [Errno 8] Exec Format Error in Python

The subprocess module in Python allows you to run commands by creating new processes. When running a shell script using its methods, sometimes you may get an OSError: [Errno 8] Exec format error in Linux.

The problem Exec format error is raised when the script is directly run and not through the correct interpreter. It occurs if there is no shebang line at the beginning of the script file.

This tutorial will teach you to fix Linux’s OSError: [Errno 8] Exec format error.

Recreate OSError: [Errno 8] Exec format error in Linux

First, let’s recreate OSError: [Errno 8] Exec format error in Linux.

The following is a Bash script myshell.sh that returns Welcome to DelftStack Tutorials.

echo "Welcome to DelftStact Tutorials"

Below is a Python script, myscript.py, that runs the above script using subprocess.Popen().

import subprocess

shell_file = "/home/delft/myshell.sh"
P = subprocess.Popen(shell_file)

Run the Python script in the terminal.

python3 script.py

Output:

Traceback (most recent call last):
  File "myscript.py", line 3, in <module>
    P = subprocess.Popen(shell_file)
  File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.8/subprocess.py", line 1704, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: '/home/delft/myshell.sh'

As you can see, it returns the error OSError: [Errno 8] Exec format error.

Add #!/bin/sh to Fix OSError: [Errno 8] Exec format error in Linux

The best way to solve this issue is by adding #!/bin/sh at the top of the shell script file myshell.sh. It ensures that the system uses the right interpreter to run the .sh script.

Edit the myshell.sh file with any editor and add the below lines.

#!/bin/sh
echo "Welcome to DelftStack Tutorials"

Now run the Python script to see the result.

python3 myscript.py

Output:

Welcome to DelftStack Tutorials

Use sh to Fix OSError: [Errno 8] Exec format error in Linux

You can also specify sh in Python script in the command that runs the shell script file.

Here is an example of it.

import subprocess

shell_file = "/home/delft/myshell.sh"
P = subprocess.Popen(["sh", shell_file])

Next, run the Python script file.

python3 myscript.py

Output:

Welcome to DelftStack Tutorials

Now you know how to solve OSError: [Errno 8] Exec format error and run shell scripts using Python in Linux. We hope you find this tutorial helpful.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - Python Error