Python, Flask, Gunicorn Error: Unrecognized Arguments

Python, Flask, Gunicorn Error: Unrecognized Arguments

我正在 运行使用 Python 和 Flask 安装我的应用程序 app.py。我正在尝试将它部署到 Heroku,我遵循了 this tutorial 中的步骤,包括制作 Procfile 和 requirements.txt。但是,每当我 运行 heroku local 时,我都会收到以下错误:

web.1  | [2015-09-26 17:36:32 -0400] [19422] [INFO] Starting gunicorn 19.3.0
web.1  | [2015-09-26 17:36:32 -0400] [19422] [INFO] Listening at: http://0.0.0.0:5000 (19422)
web.1  | [2015-09-26 17:36:32 -0400] [19422] [INFO] Using worker: sync
web.1  | [2015-09-26 17:36:32 -0400] [19425] [INFO] Booting worker with pid: 19425
web.1  | usage: gunicorn [-h] [--auth_host_name AUTH_HOST_NAME]
web.1  | gunicorn: error: unrecognized arguments: app:app
web.1  | [2015-09-26 17:36:32 -0400] [19425] [INFO] Worker exiting (pid: 19425)

我以前在 Heroku 上成功部署过应用程序,但从未遇到过此错误。我的 Procfile 只是一行:web: gunicorn app:app.

谁能告诉我如何解决这个问题?

更新: 修改了我的一些代码,现在当我 运行 heroku local 时,它 运行 没问题:

web.1  | [2015-09-28 18:52:13 -0400] [70650] [INFO] Starting gunicorn 19.3.0
web.1  | [2015-09-28 18:52:13 -0400] [70650] [INFO] Listening at: http://0.0.0.0:5000 (70650)
web.1  | [2015-09-28 18:52:13 -0400] [70650] [INFO] Using worker: sync
web.1  | [2015-09-28 18:52:13 -0400] [70653] [INFO] Booting worker with pid: 70653

但是,当我部署我的 Heroku 应用程序时,出现应用程序错误,当我检查日志时,我看到了与以前相同的错误:

2015-09-28T22:50:54.775077+00:00 app[web.1]: 2015-09-28 22:50:54 [3] [INFO] Starting gunicorn 18.0
2015-09-28T22:50:54.776176+00:00 app[web.1]: 2015-09-28 22:50:54 [3] [INFO] Using worker: sync
2015-09-28T22:50:54.776052+00:00 app[web.1]: 2015-09-28 22:50:54 [3] [INFO] Listening at: http://0.0.0.0:24995 (3)
2015-09-28T22:50:54.786067+00:00 app[web.1]: 2015-09-28 22:50:54 [9] [INFO] Booting worker with pid: 9
2015-09-28T22:50:56.004336+00:00 heroku[web.1]: State changed from starting to up
2015-09-28T22:51:42.659042+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/" host=bobawithjames.herokuapp.com request_id=1afab4c0-484e-456b-be05-3086ee0711cd fwd="160.39.250.29" dyno=web.1 connect=1ms service=39ms status=503 bytes=0
2015-09-28T22:51:42.604331+00:00 app[web.1]:                 [--noauth_local_webserver]
2015-09-28T22:51:42.604323+00:00 app[web.1]: usage: gunicorn [-h] [--auth_host_name AUTH_HOST_NAME]
2015-09-28T22:51:42.604335+00:00 app[web.1]:                 [--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]]
2015-09-28T22:51:42.633611+00:00 app[web.1]: gunicorn: error: unrecognized arguments: hello:app

有人知道现在发生了什么吗?

我根据@euxneks 提出的建议以及 Google OAuth 2.0.

的建议解决了我的问题。

本质上,我一直在使用的教程 Python Quickstart for Google Calendar API, was using argparse to get flags for credentials. However, it was also calling tools.run, which is deprecated. So instead, I decided to follow a different, more up-to-date tutorial 将引导您通过 Python Web 应用程序使用 OAuth 2.0。

我在关注 tutorial 时遇到了同样的问题 运行。我删除了他们示例中显示的 --log-file 标志。 此外,我还修改了字符串,使其引用我的应用程序目录 [/app/wsgi.py] 下的 wsgi.py 应用程序,如下所示:

web: gunicorn app.wsgi

希望对您有所帮助!

问题在于我的脚本中的 argparse 被 flask/gunicorn 运行。把这些放在一个:

if __name__ == "__main__":
    import argparse
    ...

这样,如果直接 运行,您仍然可以单独解析参数 运行。

我可以通过在我的应用程序中用 args 替换 args = parser.parse_args() 来解决这个问题,unknown = parser.parse_known_args()