Subprocess.Popen(): Oserror: [Errno 8] Exec Format Error in Python

OSError: [Errno 8] Exec format error when calling subprocess.Popen on an exe file

subprocess can only start programs that your operating system knows how to execute.

Your taxsim35-unix.exe is a Linux executable. MacOS cannot run them.

You'll need to either use a Linux machine to run this executable (real or virtual), or get a version compiled for Mac. https://back.nber.org/stata//taxsim35/taxsim35-osx.exe is likely to be the latter.

OSError: [Errno 8] Exec format error when running subprocess.Popen

From the README on the GitHub repo you linked:

Requirements

  • Linux: all experiments were executed in Linux-based machines, Ubuntu distributions, more specifically

And, if you look at that path inside the repo, mrbpr.bin is a Linux executable binary. You can't run that on a Mac.

If they provide the source to build that executable yourself, or a link to where to find it, you could probably build a Mac version. But, failing that, there's nothing you can do to fix it.


The best option would probably be to run a Linux container, virtual machine, or user-mode installation (either on your Mac, or on some free cloud host), install Python and all of the other requirements into that, and run the code that way.


If you're wondering why you got that particular error:

OSError: [Errno 8] Exec format error

There are a variety of different executable formats out there: a.out, ELF, mach-O, COFF, etc. Most linux executables are in ELF format. macOS's loader only knows mach-O and a.out. So, your OS can't even figure out what the file is, only that it's not a file it knows how to handle. But, even if you got past that, linux and Darwin syscalls are different, glibc and BSD libc are different, etc., so it would just quickly segfault anyway.

OSError: [Errno 8] Exec format error when trying to run simple flask app in a docker container

I hit the same problem (Exec format error, then FileNotFound if I added the shebang).

Adding "RUN chmod 644 app.py" to the Dockerfile fixed it for me, as mentioned here: https://github.com/pallets/werkzeug/issues/1482

Flask CLI throws 'OSError: [Errno 8] Exec format error' when run through docker-compose

Looks like your api/manage.py doesn't have a shebang ([Wikipedia]: Shebang (Unix)), so the default (current) command processor (a shell - typically bash) is attempting to run it, which (obviously) fails.

To correct the problem, add a shebang (at the beginning of the file, making sure that your editor adds the Nix style line ending (\n, 0x0A, LF)):

  • Default Python installation:

    #!/usr/bin/env python
    • Variant (specify Python 3 explicitly):

      #!/usr/bin/env python3
  • Custom Python installation:

    #!/full/path/to/your/custom/python/executable

Note that you also need exec permissions on the file (chmod +x api/manage.py).

Example:

[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q055271912]> ~/sopr.sh
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###

[064bit prompt]> ls
code00.py code01.py
[064bit prompt]>
[064bit prompt]> cat code00.py
print("This is:", __file__)

[064bit prompt]> python3 -c "import os, subprocess;subprocess.Popen(os.path.join(os.getcwd(), \"code00.py\")).communicate()"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.6/subprocess.py", line 1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: '/cygdrive/e/Work/Dev/StackOverflow/q055271912/code00.py'
[064bit prompt]>
[064bit prompt]> cat code01.py
#!/usr/bin/env python3

print("This is:", __file__)

[064bit prompt]> python3 -c "import os, subprocess;subprocess.Popen(os.path.join(os.getcwd(), \"code01.py\")).communicate()"
This is: /cygdrive/e/Work/Dev/StackOverflow/q055271912/code01.py

Another way would be to run the interpreter followed by the file name, but I don't know how to do it from Flask - actually that would require patching Werkzeug (_reloader.py: _get_args_for_reloading), but that would be just a lame workaround (gainarie) - see below.



Update #0

Looking at @AxelGrytt's answer, it turns out it's a known issue: [GitHub]: pallets/werkzeug - 0.15.0 causes OSError: [Errno 8] Exec format error: in Docker for Windows (hmm, submitted in the same day as this question (and 2 days after the release) :) ).

So, what I have stated above is correct, but it is worth mentioning that there is another way of fixing it: removing the exec permission for the file:

chmod -x api/manage.py

According to Werkzeug authors, from now on, this is desired behavior (also applies to v0.15.2):

  • A file with exec permission set, should also have a shebang
  • A file without a shebang, shouldn't have the exec permission set

Flask debug mode gives an OSError: [Errno 8] Exec format error when running using python

It looks like Flask is trying to run ./flaskblog.py directly for some reason, rather than with the python binary (python3 flaskblog.py), which is not working since flaskblog.py isn't executable.

So just add the following line (shebang) at the top of flaskblog.py

#!/usr/bin/env python3

...and make the file executable:

chmod +x flaskblog.py

Then try again, either with python3 flaskblog.py or directly as ./flaskblog.py.



Related Topics



Leave a reply



Submit