Python – import pymongo works in Python interpreter but not script

mongodbpymongopython

I first installed pymongo using easy_install, that didn't work so I tried with pip and it is still failing.

This is fine in the terminal:

Macintosh:etc me$ python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 14:13:39) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>> 

But on line 10 of my script

import pymongo

throws the following error:

File "test.py", line 10, >in <module>
import pymongo
ImportError: No module named pymongo

I'm using the standard Lion builds of Apache and Python. Is there anybody else who has experienced this?

Thanks

EDIT: I should also mention that during install it throws the following error

Downloading/unpacking pymongo
Downloading pymongo-2.1.1.tar.gz (199Kb): 199Kb downloaded
Running setup.py egg_info for package pymongo

Installing collected packages: pymongo
Running setup.py install for pymongo
building 'bson._cbson' extension
gcc-4.0 -fno-strict-aliasing -fno-common -dynamic -arch ppc -arch i386 -g -O2 -DNDEBUG -g -O3 -Ibson -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c bson/_cbsonmodule.c -o build/temp.macosx-10.3-fat-2.7/bson/_cbsonmodule.o
unable to execute gcc-4.0: No such file or directory
command 'gcc-4.0' failed with exit status 1

**************************************************************
WARNING: The bson._cbson extension module could not
be compiled. No C extensions are essential for PyMongo to run,
although they do result in significant speed improvements.

If you are seeing this message on Linux you probably need to
install GCC and/or the Python development package for your
version of Python. Python development package names for popular
Linux distributions include:

RHEL/CentOS: python-devel
Debian/Ubuntu: python-dev

Above is the ouput showing how the compilation failed.
**************************************************************

building 'pymongo._cmessage' extension
gcc-4.0 -fno-strict-aliasing -fno-common -dynamic -arch ppc -arch i386 -g -O2 -DNDEBUG -g -O3 -Ibson -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c pymongo/_cmessagemodule.c -o build/temp.macosx-10.3-fat-2.7/pymongo/_cmessagemodule.o
unable to execute gcc-4.0: No such file or directory
command 'gcc-4.0' failed with exit status 1

**************************************************************
WARNING: The pymongo._cmessage extension module could not
be compiled. No C extensions are essential for PyMongo to run,
although they do result in significant speed improvements.

If you are seeing this message on Linux you probably need to
install GCC and/or the Python development package for your
version of Python. Python development package names for popular
Linux distributions include:

RHEL/CentOS: python-devel
Debian/Ubuntu: python-dev

Above is the ouput showing how the compilation failed.
**************************************************************

And then goes on to say

Successfully installed pymongo
Cleaning up...
Macintosh:etc me$  

Very odd.

My sys.path in script is returned as:

['/Library/WebServer/Documents/', '/Library/Python/2.7/site-packages/tweepy-1.7.1-py2.7.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']

And in interpreter:

['', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SQLObject-1.2.1-py2.7.egg', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/FormEncode-1.2.4-py2.7.egg', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c12dev_r88846-py2.7.egg', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.1-py2.7.egg', '/Library/Python/2.7/site-packages/tweepy-1.7.1-py2.7.egg', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '/Library/Python/2.7/site-packages']

Best Answer

Found it! Required a path append before importing of the pymongo module

sys.path.append('/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages')
import pymongo

Would ideally like to find a way to append this to the pythonpath permanently, but this works for now!

Related Topic