pymongo “ImportError: cannot import name MongoClient”

(更新

I decided to post this in English because other people may be also struggling with the issue.

When I installed pymongo (Python MongoDB client) 3.7.2 for Python 2.7 on Debian 9, I found a strange phenomenon.  Writing the following script, saved it as ‘copy.py’ and run by

$ python2 copy.py

but it didn’t work and simply says

ImportError: cannot import name MongoClient

I googled for hours and tried provided many solutions but no luck.  The code is very simple.

from pymongo import MongoClient
client = MongoClient('localhost')

After all, the problem was the file name ‘copy.py’!  When I changed the file name to a different one, the issue was resolved!  Curious.

But in this old post on stackoverflow, Kavya_D was suggesting there seemed some kind of name conflict problem in a pymongo’s name space.  Bingo!  I hope this “strange behavior” will be officially clarified somewhere!

2019年3月18日 カテゴリー: 未分類