ImportError: No module named 'pymongo'

Question:

I have a problem running pymongo on Win 7 (64) with Python 3.4, mongodb 4.2.10.
The error output is as follows:

import pymongo
ImportError: No module named 'pymongo'

The code is pretty simple:

import pymongo
from pymongo import MongoClient

client=MongoClient()
db=client.test_db
dict={'A':[1,2,3,4,5,6]}
db.test_collection.insert(dict)
to_print=db.test_collection.find()
print(to_print)

I tried already re-installing Python and MongoDB – did not help. It works when I do it manually in cmd, i.e. mongod.exe and mongo.exe work fine. It appears there is problem with pymongo, but I don’t know how to fix it.

Asked By: user3151858

||

Answers:

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:

Answered By: Salvador Dali

I am new to Python,

But I think install setuptools is a good idea,

after that:

pip install pymongo
Answered By: vanduc1102

Try this:

  1. sudo apt-get install python-pip

  2. sudo pip install pymongo

Answered By: user7527153

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.

Answered By: Utsav Chokshi
  1. Make a new folder in your documents like “flask-pymongo”
  2. In your terminal change directory to C:UsersYOUR_USERNAMEDocumentsFlask-PyMongo
  3. git clone https://github.com/dcrosta/flask-pymongo
  4. cd flask-pymongo
  5. py setup.py develop OR python setup.py develop (depends how python was install to your path)
  6. from anywhere in your terminal use : pip install pymongo

Solution is for windows users

Answered By: FlyingZipper

If you have a problem like that probably you didn’t two things.

  1. you didn’t install pymongo.You can install by below command;

    $pip install pymongo

  2. You installed pymongo but You have two python packages location. like below;

    • C:PythonPython37-32Libsite-packagespymongo (pymongo installed here)

    • C:Anaconda3Libsite-packages “pymongo is not here”

    • And you try to work here.

    • Probably you run Spyder but Spyder is looking to AnacondaLibsite-packages but pymongo packages are not here.

Sorry for bad english.

Answered By: Developer33

I was working on Python 3 but installed the Python 2 version of pymongo with

$ pip install pymongo command. I uninstalled this version of pymongo with

$ pip uninstall pymongo and installed Python 3 version of it via

$ pip3 install pymongo. (after installing pip3 via $ sudo-apt install pip3 in linux terminal). I hope this solves your problem as well as mine.

For me I had this error after I’d installed pymongo via console/terminal but when I looked in my project’s interpreter (for example in PyCharm you go to

Preferences > ‘Project: <‘name of your project’>’ > Project Interpreter

I saw things like pip and setuptools but not pymongo. I clicked the ‘+’ at the bottom of the pane and searched for pymongo and found it and could install it there. After adding this the project ran fine

enter image description here

enter image description here

Answered By: Chris Klingler

Try the following:

conda install pymongo
Answered By: Átila Soares

For me i am running Flask server so i had to go to the terminal and run the command :

pip install Flask-PyMongo
Answered By: Simba Rashe

I had the same error on linux while working on some project,I’ll post for you the linux commands for it and you can find the windows equivalent easily

what solved my problem is to first install virtual environment venv

> sudo pacman -S python-virtualenv

create a venv (it’s in best practice to keep virtualenv in different dir than the project since you don’t need to distribute it) and activate it.

> python -m venv venv/
> . venv/bin/activate

once you are in the venv you should see the command prompt like this one

(venv) [sam@archlinux labs]$ 

now install pymongo inside your venv using

(venv) [sam@archlinux labs]$ pip install pymongo

then run your files while the virtual environment is active

(venv) [sam@archlinux labs]$ python myfile.py

to install flask also the same inside your venv using

(venv) [sam@archlinux labs]$ pip install flask
Answered By: SamyAbdellatif

I’m working with Python’s virtual environment (venv) and for some reason it didn’t work for me to just

pip install pymongo

in my venv. It installed the package correctly in venv/Lib/site-packages put I couldn’t run the script.
What worked for me was to create a requirements.txt file and write the packages I needed for the project in there

pymongo==4.0.1

and then run the command

python3 -m pip install -r requirements.txt

I can now run my script. Hope this can help any newcomers to this question.

Answered By: Jens Jensen

Whenever I’ve got an issue as this, I open a new terminal and cd into the directory of my project. Do not activate your virtualenv yet. Now, install the missing module, activate your virtualenv, case closed.

Answered By: Chukwunazaekpere
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.