关于python:ImportError:无法导入名称_UNPACK_INT

ImportError: cannot import name _UNPACK_INT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from flask import Flask
from flask import render_template
from pymongo import MongoClient
import json
from bson import json_util
from bson.json_util import dum

app = Flask(__name__)


MONGODB_HOST = 'localhost'
MONGODB_PORT = 27017
DBS_NAME = 'donorschoose'
COLLECTION_NAME = 'projects'
FIELDS = {'school_state': True, 'resource_type': True, 'poverty_level': True, 'date_posted': True, 'total_donations': True, '_id': False}

@app.route('/')
def index():
    return render_template("index.html")

@app.route("/donorschoose/projects")
def donorschoose_projects():
    connection = MongoClient(MONGODB_HOST, MONGODB_PORT)
    collection = connection[DBS_NAME][COLLECTION_NAME]
    projects = collection.find(projection=FIELDS)
    json_projects = []
    for project in projects:
        json_projects.append(project)
    json_projects = json.dumps(json_projects, default=json_util.default)
    connection.close()
    return json_projects


if __name__ =="__main__":
    app.run(host='0.0.0.0',port=5000,debug=True)

这是我下面的教程:http://adilmoujahid.com/posts/2015/01/interactive-data-visualization-d3-dc-python-mongodb/

这是运行服务器时的错误。

Traceback (most recent call last): File"app.py", line 3, in

from pymongo import MongoClient File"/home/olalkeith/Documents/Github/myproject/venv/local/lib/python2.7/site-packages/pymongo/init.py",
line 83, in
from pymongo.collection import ReturnDocument File"/home/olalkeith/Documents/Github/myproject/venv/local/lib/python2.7/site-packages/pymongo/collection.py",
line 27, in
from bson.raw_bson import RawBSONDocument File"/home/olalkeith/Documents/Github/myproject/venv/local/lib/python2.7/site-packages/bson/raw_bson.py",
line 20, in
from bson import _UNPACK_INT, _iterate_elements ImportError: cannot import name _UNPACK_INT


我也遇到了同样的问题。尝试删除bson模块并重新安装pymongo。请参阅此答案:

https://stackoverflow.com/a/12983651/4621508