pymongo == 3.0.3:ImportError:没有名为connection的模块

时间:2015-07-06 08:24:15

标签: python pymongo pymongo-3.x

我刚刚通过pymongo==3.0.3升级到pip install --upgrade pymongo,我被ImportError淹没了:

In [2]: pymongo.version
Out[2]: '3.0.3'

In [3]: from pymongo import Connection
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-3-dd44bc3249d3> in <module>()
----> 1 from pymongo import Connection

ImportError: cannot import name Connection

In [4]: from pymongo import connection
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-4-71c9e4ec1bcd> in <module>()
----> 1 from pymongo import connection

ImportError: cannot import name connection

In [5]: import pymongo.connection.Connection
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-5-282b89157c85> in <module>()
----> 1 import pymongo.connection.Connection

ImportError: No module named connection.Connection

3 个答案:

答案 0 :(得分:1)

根据Pymongo 3.0 changelog -

  

MongoClient更改

     

MongoClient现在是独立服务器,mongos或副本集的唯一客户端类。它包括已拆分为MongoReplicaSetClient的功能:它可以连接到副本集,发现其所有成员,并监视该组以进行降级,选举和重新配置。 MongoClient现在也支持完整的ReadPreference API。

     

删除了过时的类MasterSlaveConnection,Connection和ReplicaSetConnection。

如您所见,已从pymonge 3.0中删除Connection类,请尝试使用MongoClient。有关mongoclient的信息可以在here

找到

答案 1 :(得分:0)

您可以通过执行此类操作来支持代码中的两个版本。

try:
    from pymongo.connection import Connection
except ImportError as e:
    from pymongo import MongoClient as Connection

答案 2 :(得分:0)

由于pymongo(3.0.0)不推荐使用Connection类。安装较旧版本的pymongo(2.9)使其临时工作。可以使用以下方法通过pip完成:

pip install  pymongo==2.9