gpt4 book ai didi

python - '在 heroku 上导入错误 : No module named ' , flask

转载 作者:行者123 更新时间:2023-11-28 22:39:12 25 4
gpt4 key购买 nike

enter image description here

我正在开发一个 Flask 应用程序,其结构如基于 http://code.tutsplus.com/tutorials/an-introduction-to-pythons-flask-framework--net-28822 的屏幕截图所示.为了让它在本地运行,我一直在运行 routes.py 文件。

我现在正尝试在 https://www.udacity.com/wiki/ud330/deploy 之后使用 gunicorn 在 heroku 上进行部署.按照说明,My requirements.txt 包含:

$ pip Freeze
Flask==0.10.1
Flask-Mail==0.9.1
Flask-SQLAlchemy==2.1
Flask-WTF==0.12
Jinja2==2.8
MarkupSafe==0.23
SQLAlchemy==1.0.11
WTForms==2.1
Werkzeug==0.11.3
blinker==1.4
gunicorn==19.4.5
itsdangerous==0.24

我不确定要将什么放入我的 Procfile 中,我尝试了几种变体,包括:

web: gunicorn mini:intro_to_flask 

但我在 heroku 日志中得到以下内容:

←[32m2016-01-23T17:15:39.572265+00:00 app[web.1]:←[0m ImportError: No module named mi
ni
←[32m2016-01-23T17:15:39.652562+00:00 app[web.1]:←[0m super(Application, self).ru
n()
←[32m2016-01-23T17:15:39.652583+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/gunicorn/app/base.py", line 72, in run
←[32m2016-01-23T17:15:39.652835+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/gunicorn/arbiter.py", line 347, in stop
←[32m2016-01-23T17:15:39.572260+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
←[32m2016-01-23T17:15:39.652807+00:00 app[web.1]:←[0m self.stop()
←[32m2016-01-23T17:15:39.652914+00:00 app[web.1]:←[0m time.sleep(0.1)
←[32m2016-01-23T17:15:39.653207+00:00 app[web.1]:←[0m gunicorn.errors.HaltServer: <Ha
ltServer 'Worker failed to boot.' 3>
←[32m2016-01-23T17:15:40.600197+00:00 heroku[web.1]:←[0m State changed from starting
to crashed
←[32m2016-01-23T17:15:40.576720+00:00 heroku[web.1]:←[0m Process exited with status 1

我的 runserver.py 文件还包含:

from intro_to_flask import app

app.run(debug=True)

我该如何解决这个问题?

将 Procfile 编辑为:

web: gunicorn runserver:app

现在得到:

←[32m2016-01-23T17:47:43.545434+00:00 app[web.1]:←[0m     from intro_to_flask import
app
←[32m2016-01-23T17:47:43.545435+00:00 app[web.1]:←[0m ImportError: No module named in
tro_to_flask
←[32m2016-01-23T17:47:43.545435+00:00 app[web.1]:←[0m Traceback (most recent call las
t):
←[32m2016-01-23T17:47:43.545436+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/gunicorn/arbiter.py", line 515, in spawn_worker
←[32m2016-01-23T17:47:43.545438+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/gunicorn/workers/base.py", line 122, in init_process
←[32m2016-01-23T17:47:43.545437+00:00 app[web.1]:←[0m worker.init_process()
←[32m2016-01-23T17:47:43.545438+00:00 app[web.1]:←[0m self.load_wsgi()
←[32m2016-01-23T17:47:43.545439+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/gunicorn/workers/base.py", line 130, in load_wsgi
←[32m2016-01-23T17:47:43.545440+00:00 app[web.1]:←[0m self.wsgi = self.app.wsgi()

←[32m2016-01-23T17:47:43.545440+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
←[32m2016-01-23T17:47:43.545441+00:00 app[web.1]:←[0m self.callable = self.load()

←[32m2016-01-23T17:47:43.545442+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
←[32m2016-01-23T17:47:43.545442+00:00 app[web.1]:←[0m return self.load_wsgiapp()
←[32m2016-01-23T17:47:43.545443+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
←[32m2016-01-23T17:47:43.545444+00:00 app[web.1]:←[0m return util.import_app(self

编辑2:

更改为正常的 '__init__.py' 。 intro_to_flask 现在注册为模块:

←[36m2016-01-23T18:15:18.071418+00:00 app[web.1]:←[0m     from intro_to_flask import
app
←[36m2016-01-23T18:15:18.150803+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 74, in run
←[36m2016-01-23T18:15:18.150846+00:00 app[web.1]:←[0m WSGIApplication("%(prog)s [
OPTIONS] [APP_MODULE]").run()
←[36m2016-01-23T18:15:18.071419+00:00 app[web.1]:←[0m ImportError: cannot import name
app
←[36m2016-01-23T18:15:18.071605+00:00 app[web.1]:←[0m [2016-01-23 18:15:18 +0000] [10
] [INFO] Worker exiting (pid: 10)
←[36m2016-01-23T18:15:18.150976+00:00 app[web.1]:←[0m Arbiter(self).run()
←[36m2016-01-23T18:15:18.150712+00:00 app[web.1]:←[0m Traceback (most recent call las
t):
←[36m2016-01-23T18:15:18.151078+00:00 app[web.1]:←[0m self.halt(reason=inst.reaso
n, exit_status=inst.exit_status)
←[36m2016-01-23T18:15:18.150718+00:00 app[web.1]:←[0m File "/app/.heroku/python/bin
/gunicorn", line 11, in <module>
←[36m2016-01-23T18:15:18.151098+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/gunicorn/arbiter.py", line 302, in halt
←[36m2016-01-23T18:15:18.150773+00:00 app[web.1]:←[0m sys.exit(run())
←[36m2016-01-23T18:15:18.151169+00:00 app[web.1]:←[0m self.stop()
←[36m2016-01-23T18:15:18.151172+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/gunicorn/arbiter.py", line 347, in stop
←[36m2016-01-23T18:15:18.151267+00:00 app[web.1]:←[0m time.sleep(0.1)
←[36m2016-01-23T18:15:18.150869+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/gunicorn/app/base.py", line 192, in run
←[36m2016-01-23T18:15:18.151289+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/gunicorn/arbiter.py", line 219, in handle_chld
←[36m2016-01-23T18:15:18.150923+00:00 app[web.1]:←[0m super(Application, self).ru
n()
←[36m2016-01-23T18:15:18.151342+00:00 app[web.1]:←[0m self.reap_workers()
←[36m2016-01-23T18:15:18.150943+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/gunicorn/app/base.py", line 72, in run
←[36m2016-01-23T18:15:18.151362+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/gunicorn/arbiter.py", line 464, in reap_workers
←[36m2016-01-23T18:15:18.150998+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/gunicorn/arbiter.py", line 206, in run
←[36m2016-01-23T18:15:18.151520+00:00 app[web.1]:←[0m gunicorn.errors.HaltServer: <Ha
ltServer 'Worker failed to boot.' 3>
←[36m2016-01-23T18:15:18.151455+00:00 app[web.1]:←[0m raise HaltServer(reason, se
lf.WORKER_BOOT_ERROR)
←[36m2016-01-23T18:15:19.049307+00:00 heroku[web.1]:←[0m Process exited with status 1

←[36m2016-01-23T18:15:19.061843+00:00 heroku[web.1]:←[0m State changed from starting
to crashed

编辑 3:

←[36m2016-01-23T19:14:56.910512+00:00 app[web.1]:←[0m     from intro_to_flask import
routes
←[36m2016-01-23T19:14:56.910513+00:00 app[web.1]:←[0m File "/app/intro_to_flask/rou
tes.py", line 123, in <module>
←[36m2016-01-23T19:14:56.910513+00:00 app[web.1]:←[0m app.run()
←[36m2016-01-23T19:14:56.910514+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/flask/app.py", line 772, in run
←[36m2016-01-23T19:14:56.910515+00:00 app[web.1]:←[0m run_simple(host, port, self
, **options)
←[36m2016-01-23T19:14:56.910515+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/werkzeug/serving.py", line 692, in run_simple
←[36m2016-01-23T19:14:56.910516+00:00 app[web.1]:←[0m inner()
←[36m2016-01-23T19:14:56.910516+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/werkzeug/serving.py", line 654, in inner
←[36m2016-01-23T19:14:56.910517+00:00 app[web.1]:←[0m fd=fd)
←[36m2016-01-23T19:14:56.910518+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/werkzeug/serving.py", line 548, in make_server
←[36m2016-01-23T19:14:56.910518+00:00 app[web.1]:←[0m passthrough_errors, ssl_con
text, fd=fd)
←[36m2016-01-23T19:14:56.910519+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/site-packages/werkzeug/serving.py", line 462, in __init__
←[36m2016-01-23T19:14:56.910523+00:00 app[web.1]:←[0m HTTPServer.__init__(self, (
host, int(port)), handler)
←[36m2016-01-23T19:14:56.910524+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/SocketServer.py", line 420, in __init__
←[36m2016-01-23T19:14:56.910525+00:00 app[web.1]:←[0m self.server_bind()
←[36m2016-01-23T19:14:56.910526+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/BaseHTTPServer.py", line 108, in server_bind
←[36m2016-01-23T19:14:56.910526+00:00 app[web.1]:←[0m SocketServer.TCPServer.serv
er_bind(self)
←[36m2016-01-23T19:14:56.910527+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/SocketServer.py", line 434, in server_bind
←[36m2016-01-23T19:14:56.910528+00:00 app[web.1]:←[0m self.socket.bind(self.serve
r_address)
←[36m2016-01-23T19:14:56.910529+00:00 app[web.1]:←[0m File "/app/.heroku/python/lib
/python2.7/socket.py", line 228, in meth
←[36m2016-01-23T19:14:56.910529+00:00 app[web.1]:←[0m return getattr(self._sock,n
ame)(*args)
←[36m2016-01-23T19:14:56.910548+00:00 app[web.1]:←[0m error: [Errno 98] Address alrea
dy in use
←[36m2016-01-23T19:14:56.910686+00:00 app[web.1]:←[0m [2016-01-23 19:14:56 +0000] [10
] [INFO] Worker exiting (pid: 10)
←[36m2016-01-23T19:14:57.199826+00:00 heroku[web.1]:←[0m State changed from starting
to up

最佳答案

如果您从文件 intro_to_flask 中的 flask 创建您的应用程序实例,并且您的实例变量的名称是 app:

intro_to_flask.py

...
app = Flask()
...

将Procfile改成这个

web: gunicorn intro_to_flask:app

关于python - '在 heroku 上导入错误 : No module named ' , flask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34966616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com