ImportError: No module named 'pymongo'

I am new to Python,

But I think install setuptools is a good idea,

after that:

pip install pymongo

If you have installed pymongo using following command :

sudo pip install pymongo or
sudo -E pip install pymongo 

And still you are getting import error then try to run your python script with sudo like :

sudo python example.py

If you are able to run the script this way, but not without sudo. Then there can be a problem with PYTHON_PATH or Permission issue.

Solving isssue#1 (i.e. PYTHON_PATH) : Location where pip installs packages and location where python looks for packages do not match.

So how do you find where pip install packages ? Run following command :

sudo pip show pymongo

It shows output like this :

---
Name: pymongo
Version: 3.4.0
Location: /usr/local/lib/python2.7/dist-packages

Now you know where pip install packages. Add following line in your .bashrc :

export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/dist-packages/

Run following command to execute .bashrc again :

source .bashrc

Now try to run python script without sudo. It should run.

If not then do the following :

Solving issue#2 (i.e. Permission): Allow non-root users to read and execute python pacakages.

sudo chmod -R ugo+rX /usr/local/lib/python2.7/

This should solve your all problems. You should be able to run python script without sudo.


All you need is to actually install pymongo (currently you just have mongo and python, but they do not know how to speak with each other). This page is telling you exactly what to do:

  • go to pymongo page
  • download and run installer.