16

I am unable to do this:

from pymongo import MongoClient

I get:

>>> import pymongo
>>> from pymongo import MongoClient
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name MongoClient
>>> 

I am able to import pymongo without issues.

I am running mongodb 2.2.3 and Python 2.7.

I've also tried this:

>>> connection = pymongo.MongoClient()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'MongoClient'
>>> 

What am I doing wrong?

4
  • Did you install from PyPI or from git?
    – Chris
    Jul 12, 2013 at 21:42
  • nope i dont even konw what that is Jul 12, 2013 at 21:43
  • 1
    Did you install it with pip install pymongo, easy_install pymongo, or by downloading it and running setup.py install?
    – Chris
    Jul 12, 2013 at 21:44
  • sudo apt-get install python-pymongo Jul 12, 2013 at 21:45

16 Answers 16

27

That package is probably outdated or broken. Run sudo apt-get purge python-pymongo, then sudo apt-get install python-pip, then finally sudo pip install pymongo.

0
6

According to docs, MongoClient was introduced in version 2.4. As you installed pymongo from your distribution repository, it's quite possible it's not the most recent version. Try installing it via PiP (remove the one you have installed first):

pip install pymongo
1
  • thank you!! i actually had to install the older one because i could not get the most recent one to work! Jul 12, 2013 at 21:52
4

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

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

2

I would like to suggest more robust answer:

pip show pymongo

now see where it direct you and from there simply remove the pymongo directory

rm -rf <the dir where pymongo lay>/pymongo*

Now, you can safely install pymongo again:

pip install pymongo
2

This problem may occur if you have multiple .py programs in the current working directory. Deleting them solved my error. I am not sure of the reason though.

1

yes it's true you need to update use, check the upgrade section says http://api.mongodb.org/python/current/installation.html

   easy_install -U pymongo
1

If you used pymongo.Connection and encountered error, you should know that after the new updates, you should use pymongo.MongoClient instead. I had this issue so hopefully this will be helpful to someone else.

0

I had the same problem on Ubuntu 16.04.
This solved my problem:

sudo apt-get install build-essential python-dev

sudo apt-get install python-pip

sudo pip install pymongo
2
  • some explanation about what you are doing there please?
    – ngstschr
    Jun 16, 2017 at 9:57
  • The build-essentials reference all the packages needed to compile the python-dev tools. Then install pip and install pymongo with pip. If you already have pymongo installed, you can purge pymongo and then install pymongo package in the above way. PS - I did not check it in other versions of ubuntu.
    – Ananda
    Jun 16, 2017 at 10:21
0

Python Script

Try it once, into python script run following statement

# try.py

import pymongo

If above statement didn't throw any exception then you need to install pymongo-2.4, if existing pymongo verion is 2.3

Open terminal

First you need to uninstall old pymongo version

pip uninstall pymongo

Then use following commands to install pymongo-2.4

wget http://pypi.python.org/packages/source/p/pymongo/pymongo-2.4.tar.gz
tar xzf pymongo-2.4.tar.gz
cd pymongo-2.4
python setup.py install
0

Worked for me for conda env

pip uninstall pymongo

then

pip install pymongo
0

Make sure your python source file is not called pymongo.py

1
  • This question was originally asked 7 years ago with 10 other answers?
    – Aliqua
    Aug 19, 2020 at 10:17
0

Change filename from pymongo.py to something else.

0

You have a file named queue.py in your project folder. Rename that file, and that should fix the problem.

1
0

For newer version of pymongo, you don't need to import MongoClient. Do the following:

import pymongo

client = pymongo.MongoClient("localhost", 27017)
db = client.test_database
db.test_table

You'll get:

Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'test_database'), 'test_table')
0

I had the same issue with mypy type checks for the MongoClient class of pymongo. This solved the issue:

from pymongo.mongo_client import MongoClient
0

Firstly, create a virtual environment using Python 3.11 as the library has some changes which will not work with this code.

Secondly, along with pymongo, install pymongo[srv] then try to run the code. I hope it will work now.

To create conda environment:

conda create venv python==3.9 -y

To activate environment:

conda activate venv

Then to install the required packages:

pip install pymongo

pip install pymongo[srv]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.