2

有没有使用 Requests JSON 的简单 Heroku 教程?

当我的 app.py 调用另一个使用 Request 的 json() 的 python 脚本时,Heroku 会引发依赖错误。

Heroku 日志是:

2014-03-11T17:56:57.518693+00:00 app[web.1]: 2014-03-11 17:56:57 [7] [ERROR] Error handling request
2014-03-11T17:56:57.518693+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 125, in handle_request
2014-03-11T17:56:57.518693+00:00 app[web.1]: Traceback (most recent call last):
2014-03-11T17:56:57.518693+00:00 app[web.1]:     respiter = self.wsgi(environ, resp.start_response)
2014-03-11T17:56:57.518693+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
2014-03-11T17:56:57.518693+00:00 app[web.1]:     response = self.full_dispatch_request()
2014-03-11T17:56:57.518693+00:00 app[web.1]:     response = self.make_response(self.handle_exception(e))
2014-03-11T17:56:57.518965+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request
2014-03-11T17:56:57.518693+00:00 app[web.1]:     return self.wsgi_app(environ, start_response)
2014-03-11T17:56:57.518693+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
2014-03-11T17:56:57.518693+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__
2014-03-11T17:56:57.518965+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request
2014-03-11T17:56:57.518965+00:00 app[web.1]:     rv = self.handle_user_exception(e)
2014-03-11T17:56:57.518965+00:00 app[web.1]:   File "/app/app.py", line 66, in processData
2014-03-11T17:56:57.518965+00:00 app[web.1]:     latAndLong = getLatLong.getLatLong(str(request.form.get('city')))
2014-03-11T17:56:57.518965+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1344, in dispatch_request
2014-03-11T17:56:57.518965+00:00 app[web.1]:     return self.view_functions[rule.endpoint](**req.view_args)
2014-03-11T17:56:57.518965+00:00 app[web.1]:     rv = self.dispatch_request()
2014-03-11T17:56:57.518965+00:00 app[web.1]:     myfile = r.get_json()
2014-03-11T17:56:57.519151+00:00 app[web.1]: AttributeError: 'Response' object has no attribute 'get_json'
2014-03-11T17:56:57.518965+00:00 app[web.1]:   File "/app/getLatLong.py", line 16, in getLatLong
2014-03-11T17:56:57.519447+00:00 heroku[router]: at=info method=POST path=/getsurveyresults host=censusmappingandmarketing.herokuapp.com request_id=1606f734-83bc-4d9c-a6af-8149e0f200e2 fwd="208.91.53.169" dyno=web.1 connect=1ms service=178ms status=500 bytes=238

它有问题的代码是这样的:

r = requests.get(link)
myfile = r.json()

这令人困惑,因为它在本地运行良好。

4

1 回答 1

0

这有一个非常愚蠢的解决方案。我必须在我的 requirements.txt 中导入请求。这就是它无法识别 json() 的原因(因为它是一个请求函数)。

我的 requirements.txt 现在看起来像:

Flask==0.9
Jinja2==2.6
Werkzeug==0.8.3
gunicorn==0.17.2
requests==2.2.1
于 2014-03-25T15:40:38.340 回答