如何解决导入错误

如何解决导入错误

本文介绍了导入pymongo会导致“导入错误:无法导入名称BSON".如何解决导入错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用Flask,MongoDB和Python使一个简单的Hello World应用程序正常工作.尝试从 bson 模块导入时,该应用程序遭到轰炸.所有模块均已通过 pip 成功安装,并且我在 virtualenv 中运行,所以我不确定为什么会出现错误: ImportError:无法导入名称BSON

I'm having trouble getting a simple Hello World app to work using Flask, MongoDB, and Python. The app is bombing when trying to import from the bson module. All modules were installed successfully via pip and I'm running in a virtualenv, so I'm not sure why I'm getting the error: ImportError: cannot import name BSON

这是我的示例应用代码:

Here is my sample app code:

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)

我不确定如何调试此功能,或者我是否在这里缺少任何内容.

I'm not sure how to go about debugging this or if there's something I'm missing here.

pip冻结的结果:

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 .立即完成跟踪:

Removed bson and pymongo, reran pip install pymongo. Complete trace now:

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

推荐答案

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

Try uninstalling the bson and pymongo packages and then reinstalling the pymongo package. pymongo installs its own bson package and I think you've overwritten it with the other bson package installation and that is probably what is causing your import error.

仅供参考:

  • 当您通过pip安装pymongo时,它也会同时安装bson,但它从未打印出一条消息,指出它是将bson安装到您的virtualenv站点软件包中.
  • pymongo也不会将bson列为dep
  • 也pip -v列表未显示bson已安装
  • 这些项目符号IMO有一些代码味道(至少打印一条消息,表明您正在安装bson)

这篇关于导入pymongo会导致“导入错误:无法导入名称BSON".如何解决导入错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 00:02