25

我无法让一个简单的 Hello World 应用程序使用 Flask、MongoDB 和 Python 工作。尝试从bson模块导入时,应用程序正在轰炸。所有模块都通过成功安装pip并且我正在运行virtualenv,所以我不确定为什么会出现错误:ImportError: cannot import name BSON

这是我的示例应用程序代码:

import os
from flask import Flask
from flask import g
from flask import jsonify
from flask import json
from flask import request
from flask import url_for
from flask import redirect
from flask import render_template
from flask import make_response
import pymongo
from pymongo import Connection
from bson import BSON
from bson import json_util

app = Flask(__name__)

@app.route('/')
def hello():

    connection = Connection()
    # Get your DB
    db = connection.my_database
    # Get your collection
    cars = db.cars
    # Create some objects
    import datetime
    car = {"brand": "Ford",
           "model": "Mustang",
           "date": datetime.datetime.utcnow()}
    # Insert it
    cars.insert(car)
    json_docs = [json.dumps(doc, default=json_util.default) for doc in cars.find()]

    return json_docs


if __name__ == '__main__':
    # Bind to PORT if defined, otherwise default to 5000.
    port = int(os.environ.get('PORT', 5000))
    app.debug = True
    app.run(host='0.0.0.0', port=port)

我不确定如何进行调试,或者我在这里是否缺少某些东西。

编辑:结果pip freeze

Flask==0.9
Jinja2==2.6
WTForms==1.0.2
Werkzeug==0.8.3
bson==0.3.3
distribute==0.6.28
mongo==0.2.0
pymongo==2.3
pytz==2012g
wsgiref==0.1.2

Edit2:删除了 bson 和 pymongo,重新运行pip install pymongo。现在完成跟踪:

Traceback (most recent call last):
18:21:20 web.1  |   File ".../venv/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__
18:21:20 web.1  |     return self.wsgi_app(environ, start_response)
18:21:20 web.1  |   File ".../venv/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
18:21:20 web.1  |     response = self.make_response(self.handle_exception(e))
18:21:20 web.1  |   File ".../venv/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
18:21:20 web.1  |     response = self.full_dispatch_request()
18:21:20 web.1  |   File ".../venv/lib/python2.7/site-packages/flask/app.py", line 1361, in full_dispatch_request
18:21:20 web.1  |     response = self.make_response(rv)
18:21:20 web.1  |   File ".../venv/lib/python2.7/site-packages/flask/app.py", line 1450, in make_response
18:21:20 web.1  |     rv = self.response_class.force_type(rv, request.environ)
18:21:20 web.1  |   File ".../venv/lib/python2.7/site-packages/werkzeug/wrappers.py", line 711, in force_type
18:21:20 web.1  |     response = BaseResponse(*_run_wsgi_app(response, environ))
18:21:20 web.1  |   File ".../venv/lib/python2.7/site-packages/werkzeug/test.py", line 818, in run_wsgi_app
18:21:20 web.1  |     app_iter = app(environ, start_response)
18:21:20 web.1  | TypeError: 'list' object is not callable
4

3 回答 3

80

尝试卸载bsonpymongo包,然后重新安装pymongo包。pymongo安装它自己的bson包,我认为你已经用其他bson包安装覆盖了它,这可能是导致你的导入错误的原因。

供参考:

  • 当您 pip install pymongo 它也会安装 bson 但它不会打印一条消息说它正在将 bson 安装到您的 virtualenv 站点包
  • pymongo 也没有将 bson 列为 dep
  • pip -v list 也没有显示安装了 bson
  • 那些要点 IMO 有一些代码气味(至少打印一条消息,说明您正在安装 bson)
于 2012-10-19T23:13:03.833 回答
25

我遇到了同样的问题。我认为原因是我安装pymongo然后安装bson. 然后我卸载bson。然后我遇到了这个问题。

pip freeze pymongo它什么都不需要。

所以也许它有自己的 bson 包。

我解决这个问题的方法:

pip uninstall pymongo

pip uninstall bson

然后重新安装pymongo

pip install pymongo

于 2017-09-12T00:17:35.093 回答
6

您正在返回一个列表hello()。您需要返回一个字符串或一个 Response 对象,请参阅Flask 文档。例如,在返回之前将列表转换为字符串:

return '\n'.join(json_docs)
于 2012-10-19T23:30:47.070 回答