Reputation: 820
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.
Upvotes: 42
Views: 193531
Reputation: 6245
I am new to Python,
But I think install setuptools is a good idea,
after that:
pip install pymongo
Upvotes: 27
Reputation: 1012
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.
Upvotes: 0
Reputation: 21
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.
Upvotes: 2
Reputation: 56
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
Upvotes: 2
Reputation: 39
For me i am running Flask server so i had to go to the terminal and run the command :
pip install Flask-PyMongo
Upvotes: 4
Reputation: 5296
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
Upvotes: 2
Reputation: 111
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.
Upvotes: 8
Reputation: 97
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 pymongo
You 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.
Upvotes: 5
Reputation: 711
Solution is for windows users
Upvotes: 3
Reputation: 1395
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.
Upvotes: 10
Reputation: 61
Try this:
sudo apt-get install python-pip
sudo pip install pymongo
Upvotes: 6
Reputation: 222541
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:
Upvotes: 32