Ismael Moral
Ismael Moral

Reputation: 732

Pymongo issue in a python project

I have installed pymongo, and when I start a new project and I write import pymongo Python shows me the next error.

Python version: 3.4.3

Error code

Traceback (most recent call last):
  File "TestMongoDB.py", line 3, in <module>
    import pymongo
ImportError: No module named 'pymongo'

Pymongo

If I put this code python -c "import pymongo; print(pymongo.version); print(pymongo.has_c())" into the terminal, it shows the next information.

3.0rc1
True

I'm trying to execute my python code via terminal with this command

python3 TestMongoDB.py

Input

python -c "import pymongo; import sys; print(pymongo.version); print(pymongo.has_c()); print (sys.path)"

Output

3.0rc1
True
['', '/Library/Python/2.7/site-packages/pip-6.1.1-py2.7.egg', '/Library/Python/2.7/site-packages/pymongo-3.0rc1-py2.7-macosx-10.10-intel.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']

Input python code

# Include mongo library.
import pymongo

# Include sys library.
import sys

print (sys.path)

Output python code

['/Users/ismaelmoral/python/TestMongoDB', '/Library/Python/2.7/site-packages/pip-6.1.1-py2.7.egg', '/Library/Python/2.7/site-packages/pymongo-3.0rc1-py2.7-macosx-10.10-intel.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']

Upvotes: 2

Views: 262

Answers (1)

J Richard Snape
J Richard Snape

Reputation: 20344

It seems that you have installed your pymongo for Python 2, but you are trying to run it using Python 3. You need to install it for Python 3.

You can see that the command that works uses python whereas the one that fails uses python3. You can verify that this is the problem by checking that this works:

python testMongoDB.py

and this fails:

python3 -c "import pymongo; print(pymongo.version); print(pymongo.has_c())"

Solution

I'll assume you use pip to install your packages. You need to use pip for your Python 3 installation rather than the one that is on your default path for Python 2.

Go to the directory \root\dir\for\Python3X\Scripts. Run the pip that you find in there, e.g. ./pip install pymongo and that will install it for Python 3

If that solution doesn't meet your needs - have a look at the Pymongo installation docs.

Upvotes: 1

Related Questions