Solving Flask CLI Python Error – OSError: [Errno 8] Exec format error

Flask CLI Python Error – OSError: [Errno 8] Exec format error is an error which occurs because api/manage.py doesn’t have a shebang.

This post is my attempt to explain to you why this error occurs and how you can solve it, I will also include multiple solutions that could be considered as alternative fixes to the error.

Exploring the Error : Flask CLI Python Error – OSError: [Errno 8] Exec format error

The Exec format error occurs in Flask because api/manage.py doesn’t have a shebang.

The error should look like this. Double check in order to avoid mixing between errors.

                                                                       #
Traceback (most recent call last):
File "manage.py", line 22, in <module>
cli()
....
File "/usr/local/lib/python3.6/subprocess.py", line 1364, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: '/api/manage.py'
                                                                       #

Bellow are the solutions which worked for me and will help you to successfully solve your problem.

Solution 1 : Downgrade Werkzeug.

This is a problem that could be fixed with changing your code or by downgrading Werkzeug. If having a certain Werkzeug version does not matter to you. You can try downgrading Werkzeug.

Like any other package or library you should uninstall your current version and then install an older version.

You can find all versions of Werkzeug in the link bellow

https://werkzeug.palletsprojects.com/en/2.2.x/

If this solution does not fix your problem you can try editing your code with the help of the options bellow.

Solution 2 :  remove the exec permission for the file

The second solution is to remove the exec permission for the file

According to the developers behind Werkzeug.

You shouldn’t have the exec permission set for files without a shebang. While you should have a shebang for files who have exec permission set.

This is how you can remove the exec permission for the file

                                                                       #
chmod -x api/manage.py
                                                                       #

The solution should be enough, please try the final solution if this fails.

Solution 3 : add a shebang at the beginning of the file

The solution is to add a shebang at the beginning of the file depending on how you installed python

                                                                       #
For the Custom Python installation:
#!/full/path/to/your/custom/python/executable

For the Default Python installation:
#!/usr/bin/env python
                                                                       #

And finally For Python 3

                                                                       #
#!/usr/bin/env python3
                                                                       #

I hope this has solved your issue.

If this article has been useful for your particular case, consider donating to our Kofi account, there is a red button at the top of this page.

Summing-up

The article is over, I hope I have been able to help you solve this error or at least guide you in the right direction, check out other solutions to different errors you can do that by using the search bar on top of this page.

Keep learning, keep coding guys, cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/