ImportError: No module named 'pymongo'
Olivia Zamora
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.
13 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:
- go to pymongo page
- download and run installer.
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.pyIf 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 pymongoIt shows output like this :
---
Name: pymongo
Version: 3.4.0
Location: /usr/local/lib/python2.7/dist-packagesNow 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 .bashrcNow 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.
1I 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.
Try this:
sudo apt-get install python-pipsudo pip install pymongo
If you have a problem like that probably you didn't two things.
you didn't install pymongo.You can install by below command;
$
pip install pymongoYou installed pymongo but You have two python packages location. like below;
C:\Python\Python37-32\Lib\site-packages\pymongo(pymongo installed here)C:\Anaconda3\Lib\site-packages\"pymongo is not here"And you try to work here.
Probably you run Spyder but Spyder is looking to Anaconda\Lib\site-packages\ but pymongo packages are not here.
Sorry for bad english.
For me i am running Flask server so i had to go to the terminal and run the command :
pip install Flask-PyMongo - Make a new folder in your documents like "flask-pymongo"
- In your terminal change directory to C:\Users\YOUR_USERNAME\Documents\Flask-PyMongo\
- git clone
- cd flask-pymongo
- py setup.py develop OR python setup.py develop (depends how python was install to your path)
- from anywhere in your terminal use : pip install pymongo
Solution is for windows users
Try the following:
conda install pymongo 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
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-virtualenvcreate 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/activateonce 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 pymongothen run your files while the virtual environment is active
(venv) [sam@archlinux labs]$ python myfile.pyto install flask also the same inside your venv using
(venv) [sam@archlinux labs]$ pip install flask 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.
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.