我的原始应用是这样的(以前不需要此处的路线):
if __name__ == "__main__" :
parser = argparse.ArgumentParser()
parser.add_argument('dir', type=str, help='specify the images directory')
.....
现在将python文件本身称为app.py
,以便将我的应用程序部署到heroku上,我在其中添加了一个文件Procfile
文件,其中包含以下语句:web: gunicorn app:app
,并将以下内容添加到以前共享的代码行中:
app = Flask(__name__)
@app.route('/')
#if __name__ == "__main__" :
def index():
parser = argparse.ArgumentParser()
parser.add_argument('dir', type=str, help='specify the images directory')
我在网页上看到的错误是:
Application error
An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details.
调试显示:
2018-07-11T12:03:06.041871+00:00 app[web.1]: [2018-07-11 12:03:06 +0000] [9] [ERROR] Exception in worker process
2018-07-11T12:03:06.035624+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
2018-07-11T12:03:06.035631+00:00 app[web.1]: if __name__ == "__main__" :
2018-07-11T12:03:06.035639+00:00 app[web.1]: SyntaxError: invalid syntax
2018-07-11T12:03:06.041875+00:00 app[web.1]: Traceback (most recent call last):
2018-07-11T12:03:06.041877+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2018-07-11T12:03:06.041879+00:00 app[web.1]: worker.init_process()
2018-07-11T12:03:06.041881+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process
2018-07-11T12:03:06.041882+00:00 app[web.1]: self.load_wsgi()
2018-07-11T12:03:06.041884+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
2018-07-11T12:03:06.041885+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2018-07-11T12:03:06.041887+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2018-07-11T12:03:06.041889+00:00 app[web.1]: self.callable = self.load()
2018-07-11T12:03:06.041892+00:00 app[web.1]: return self.load_wsgiapp()
2018-07-11T12:03:06.041894+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
2018-07-11T12:03:06.041890+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
2018-07-11T12:03:06.041895+00:00 app[web.1]: return util.import_app(self.app_uri)
2018-07-11T12:03:06.041897+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app
2018-07-11T12:03:06.041898+00:00 app[web.1]: __import__(module)
2018-07-11T12:03:06.041900+00:00 app[web.1]: File "/app/app.py", line 121
2018-07-11T12:03:06.041901+00:00 app[web.1]: if __name__ == "__main__" :
2018-07-11T12:03:06.041903+00:00 app[web.1]: ^
2018-07-11T12:03:06.041910+00:00 app[web.1]: SyntaxError: invalid syntax
2018-07-11T12:03:06.042123+00:00 app[web.1]: [2018-07-11 12:03:06 +0000] [9] [INFO] Worker exiting (pid: 9)
2018-07-11T12:03:06.201688+00:00 app[web.1]: [2018-07-11 12:03:06 +0000] [4] [INFO] Shutting down: Master
2018-07-11T12:03:06.202039+00:00 app[web.1]: [2018-07-11 12:03:06 +0000] [4] [INFO] Reason: Worker failed to boot.
2018-07-11T12:03:06.328723+00:00 heroku[web.1]: Process exited with status 3
最重要的一行是Reason: Worker failed to boot
,这使我认为我没有正确启动该应用程序,有人可以引导我到那里(它指向以if开头的行,但这对我来说没有意义,因为(有评论)。
即使在删除包含if的行并推送后,我仍然收到与之相关的错误。
答案 0 :(得分:0)
检查此样板是否可以帮助您。
import argparse
from flask import Flask
def create_app(config=None):
app = Flask(__name__)
app.config.update(dict(DEBUG=True))
app.config.update(config or {})
# Definition of the routes. Put them into their own file. See also
# Flask Blueprints: http://flask.pocoo.org/docs/latest/blueprints
@app.route("/")
def hello_world():
return "Hello World"
return app
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('dir', type=str, help='specify the images directory')
parser.add_argument("-p", "--port", action="store", default="8000")
args = parser.parse_args()
port = int(args.port)
app = create_app()
app.run(host="0.0.0.0", port=port)