Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

So, here's a file I made (flaskblog.py):

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "<h1>Home Page</h1>"

Here's how I first ran it:

$ export FLASK_APP=flaskblog.py
$ flask run

Here's how I ran it in debug mode:

$ export FLASK_APP=flaskblog.py
$ export FLASK_DEBUG=1
$ flask run

Now I want to run the application directly using python. I first updated the .py file:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "<h1>Home Page</h1>"

if __name__ == "__main__":
    app.run()

This is the command I used to run the python file:

$ python3 flaskblog.py

It worked fine. Now I want to run the application in debug mode. So, I updated the file:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "<h1>Home Page</h1>"

if __name__ == "__main__":
    app.run(debug=True) #Added ("debug=True") here

Command used to run the file:

$ python3 flaskblog.py

Here's the error:

 * Serving Flask app "flaskblog" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
Traceback (most recent call last):
  File "flaskblog.py", line 9, in <module>
    app.run(debug=True)
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 943, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python3.6/dist-packages/werkzeug/serving.py", line 988, in run_simple
    run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
  File "/usr/local/lib/python3.6/dist-packages/werkzeug/_reloader.py", line 332, in run_with_reloader
    sys.exit(reloader.restart_with_reloader())
  File "/usr/local/lib/python3.6/dist-packages/werkzeug/_reloader.py", line 176, in restart_with_reloader
    exit_code = subprocess.call(args, env=new_environ, close_fds=False)
  File "/usr/lib/python3.6/subprocess.py", line 267, in call
    with Popen(*popenargs, **kwargs) as p:
  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: '/XXX/XXX/XXX/XXX/XXX/XXX/XXX/XXX/Flask_Blog/flaskblog.py'

I just used "XXX" instead of the actual directories. Any help will be appreciated!

PS: All the code is from this video: https://www.youtube.com/watch?v=MwZwr5Tvyxo&list=PL-osiE80TeTs4UjLw5MM6OjgkjFeUxCYH

like image 610
sg7610 Avatar asked Mar 24 '19 09:03

sg7610


Video Answer


1 Answers

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.

like image 161
Sumit Avatar answered Oct 31 '22 12:10

Sumit