Windows中的“ImportError:无法导入名称mongoclient”

时间:2021-08-03 23:07:34

I'm calling the following simple script to connect to a mongo database via Python.

我正在调用以下简单脚本通过Python连接到mongo数据库。

I am facing the error while running this below program in windows

我在Windows下运行以下程序时遇到错误

import pymongo

from pymongo import Connection
connection = Connection('localhost', 27017)

Here is the below error

这是以下错误

C:\Python27>python pymongo.py
Traceback (most recent call last):
  File "pymongo.py", line 2, in <module>
    import pymongo
  File "C:\Python27\pymongo.py", line 4, in <module>
    from pymongo import MongoClient
ImportError: cannot import name MongoClient

Can somebody please help me on this

有人可以帮我这个

1 个解决方案

#1


22  

You named your script pymongo.py, which masks the pymongo module from which you are importing.

您将脚本命名为pymongo.py,它掩盖了您要导入的pymongo模块。

Rename your script to something else (and delete the pymongo.pyc file if one was created next to it).

将脚本重命名为其他内容(如果在旁边创建了pymongo.pyc文件,则删除它)。

#1


22  

You named your script pymongo.py, which masks the pymongo module from which you are importing.

您将脚本命名为pymongo.py,它掩盖了您要导入的pymongo模块。

Rename your script to something else (and delete the pymongo.pyc file if one was created next to it).

将脚本重命名为其他内容(如果在旁边创建了pymongo.pyc文件,则删除它)。