3
votes

I successfully installed pymongo using pip3

Aleeshas-MacBook-Air:project 2 aleesha$ sudo pip3 install pymongo
The directory '/Users/aleesha/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/aleesha/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pymongo
  Downloading pymongo-3.4.0-cp35-cp35m-macosx_10_6_intel.whl (294kB)
    100% |████████████████████████████████| 296kB 730kB/s 
Installing collected packages: pymongo
Successfully installed pymongo-3.4.0
Aleeshas-MacBook-Air:project 2 aleesha$

Then I wrote a test script in python which writes to MongoDB. But when I execute it, I get the following error:

Aleeshas-MacBook-Air:project 2 aleesha$ python pythonScript.py 
Traceback (most recent call last):
  File "pythonScript.py", line 5, in <module>
    from pymongo import MongoClient
ImportError: No module named pymongo
Aleeshas-MacBook-Air:project 2 aleesha$

Python Version: Python 3.5.2

This is how the path looks like:

 >>> import sys
    >>> sys.path
    ['', '/Users/aleesha/Documents', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python35.zip', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages']
>>> sys.version
'3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) \n[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]'
>>> 

Searched on google. Found this question on stackoverflow: ImportError: No module named 'pymongo' which then points to another page on stackoverflow: failure to import pymongo ubuntu

sudo pip uninstall pymongo ==> Succesfully uninstalled
sudo pip uninstall bson ==> Cannot uninstall requirement bson, not installed
sudo apt-get remove python-bson ==> sudo: apt-get: command not found
sudo apt-get remove python-gridfs    # not sure if it's necessary ==> sudo: apt-get: command not found
sudo pip install pymongo -U ==> successfully installed pymongo

However even after this, I face the same issue. I am not able to import the pymongo module.

EDIT: I was trying few more things to fix the issue. Using IDLE I could import pymongo and easily insert a record into MongoDB.

>>> from pymongo import MongoClient
>>> client = MongoClient()
>>> db = client.kmp
>>> db.KMP_DATA.insert_one({"seq" : 1,"data" : "ABCD"})
<pymongo.results.InsertOneResult object at 0x1058c8828>
>>> 

However I still cannot import it from python script.

2
print sys.version and check if your Python version is actually 3.5. If not, then use python3 instead of just python and it will work fine.shad0w_wa1k3r
>>> sys.version '3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) \n[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]' >>>RDM
You installed pymongo with pip3 and running your script with python which is I guess py2.xstyvane
@styvane, I did same mistake. Installed python3/pip3 and runing script with pyhon.bhattraideb

2 Answers

14
votes

It's a common problem that "pip" or "pip3" are not the same environment as "python" or "python3". To ensure you're installing with the same environment as you run:

python -m pip install pymongo

Or:

python3 -m pip install pymongo
0
votes
  1. python path macOS have its own python. And sometimes we will install another python from brew or anaconda. So first you should which python is using.
which python
which python3
which pip
which pip3

the commands python, python3,pip and pip3, could be a path, alias or softlink.

if it's a path, you find it; if it's a alias or softlink, you should use ls -l xxx find the real path of python

example:

[morningtzh@mbajiushigelaji:~]
% which python
python: aliased to /Users/morningtzh/anaconda/bin/python3
[morningtzh@mbajiushigelaji:~]
% ls -l /Users/morningtzh/anaconda/bin/python3
lrwxr-xr-x  1 morningtzh  staff  9  1  8 14:41 /Users/morningtzh/anaconda/bin/python3 -> python3.6
[morningtzh@mbajiushigelaji:~]
% ls -l /Users/morningtzh/anaconda/bin/python3.6
-rwxrwxr-x  1 morningtzh  staff  3937816  1  8 14:41 /Users/morningtzh/anaconda/bin/python3.6
[morningtzh@mbajiushigelaji:~]
% which pip3
/Users/morningtzh/anaconda/bin/pip3
[morningtzh@mbajiushigelaji:~]
% ls -l /Users/morningtzh/anaconda/bin/pip3
-rwxr-xr-x  1 morningtzh  staff  237 10 16 16:16 /Users/morningtzh/anaconda/bin/pip3

The example shows my python and pip is a couple~~~

  1. my you using virtual environment

The same way to find the real path.