关于python:TypeError:’TextBlob’对象不可调用

TypeError: 'TextBlob' object is not callable

我可以成功地从一个简单的脚本加载textBlob模块,但不能从flask应用程序加载。我会告诉你代码和错误。

在简单脚本中加载可以工作:

1
2
3
4
5
6
from textblob import TextBlob

text = 'purfect kitten'

blob = TextBlob(text)
print blob.correct()

从Flask应用程序加载引发错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from textblob import TextBlob
from flask import (
    Flask,
    request
)

app = Flask(__name__)

@app.route('/parse', methods=['GET', 'POST'])
def parse():
    b = TextBlob(request.json['text'])
    b.correct()
    return b

if __name__ == '__main__':
    print 'running app on port 5000'
    app.debug = True
    app.run()

错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
127.0.0.1 - - [18/Nov/2015 14:54:25]"POST /parse HTTP/1.1" 500 -
Traceback (most recent call last):
  File"/Users/peter/if/root/venv/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File"/Users/peter/if/root/venv/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File"/Users/peter/if/root/venv/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File"/Users/peter/if/root/venv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File"/Users/peter/if/root/venv/lib/python2.7/site-packages/flask/app.py", line 1478, in full_dispatch_request
    response = self.make_response(rv)
  File"/Users/peter/if/root/venv/lib/python2.7/site-packages/flask/app.py", line 1577, in make_response
    rv = self.response_class.force_type(rv, request.environ)
  File"/Users/peter/if/root/venv/lib/python2.7/site-packages/werkzeug/wrappers.py", line 841, in force_type
    response = BaseResponse(*_run_wsgi_app(response, environ))
  File"/Users/peter/if/root/venv/lib/python2.7/site-packages/werkzeug/wrappers.py", line 57, in _run_wsgi_app
    return _run_wsgi_app(*args)
  File"/Users/peter/if/root/venv/lib/python2.7/site-packages/werkzeug/test.py", line 867, in run_wsgi_app
    app_rv = app(environ, start_response)
TypeError: 'TextBlob' object is not callable

我使用的是安装了PIP的textBlob 0.11.0版。


TextBlob工作正常。问题就在于你如何在你的Flask应用中使用TextBlob

烧瓶的app.route要求退回Responsestr,但您退回的是b,即TextBlob。您需要将b作为str返回

1
2
3
4
5
@app.route('/parse', methods=['GET', 'POST'])
def parse():
    b = TextBlob('hello world')
    b.correct()
    return str(b)