使用Python运行时,烧瓶调试模式会给出“ OSError:[Errno 8] Exec格式错误”

时间:2019-03-24 09:03:43

标签: python-3.x flask

所以,这是我制作的文件(flaskblog.py):

from flask import Flask
app = Flask(__name__)

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

这是我第一次运行它的方式:

$ export FLASK_APP=flaskblog.py
$ flask run

这是我在调试模式下运行它的方式:

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

现在,我想直接使用python运行应用程序。我首先更新了.py文件:

from flask import Flask
app = Flask(__name__)

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

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

这是我用来运行python文件的命令:

$ python3 flaskblog.py

效果很好。现在,我想在调试模式下运行该应用程序。因此,我更新了文件:

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

用于运行文件的命令:

$ python3 flaskblog.py

这是错误:

 * 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'

我只是使用“ XXX”而不是实际目录。任何帮助将不胜感激!

PS:所有代码均来自以下视频:https://www.youtube.com/watch?v=MwZwr5Tvyxo&list=PL-osiE80TeTs4UjLw5MM6OjgkjFeUxCYH

2 个答案:

答案 0 :(得分:6)

Flask出于某种原因似乎正在尝试直接运行#!/usr/bin/env python3 ,而不是使用python二进制文件(chmod +x flaskblog.py ),由于无法执行flaskblog.py,该二进制文件无法正常工作。

因此,只需在flaskblog.py顶部添加以下行(shebang

python3 flaskblog.py

...并使文件可执行:

./flaskblog.py

然后使用myOptionsModel$: BehaviorSubject<MyObjectType[]> = new BehaviorSubject([]); addOption(name: string, id: number) { const updatedModel = this.myOptionsModel$.value.concat([{id, name}]) this.myOptionsModel$.next(updatedModel) } 或直接以[options]="myOptionsModel$ | async" 的身份再次尝试。

答案 1 :(得分:0)

我只是将.py文件的权限从775更改为664,实际上是从权限中删除了“ x”。

更改自:

-rwxrwxr-x 1 ubuntu ubuntu 

收件人:

-rw-rw-r-- 1 ubuntu ubuntu