close

[Solved] Import: No module named ‘pymongo’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Import: No module named ‘pymongo’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Import: No module named ‘pymongo’ Error Occurs?

Today I get the following error Import: No module named ‘pymongo’ in python.

How To Solve Import: No module named ‘pymongo’ Error ?

  1. How To Solve Import: No module named 'pymongo' Error ?

    To Solve Import: No module named 'pymongo' Error Solving issue#2 (i.e. Permission): Allow non-root users to read and execute python pacakages.

  2. Import: No module named 'pymongo'

    To Solve Import: No module named 'pymongo' Error Solving issue#2 (i.e. Permission): Allow non-root users to read and execute python pacakages.

Solution 1

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.

Solution 2

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.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read