9

I'm trying to insert documents into mongodb from django and I'm getting an error on the import statement for pymongo. I don't have a duplicate file anywhere called pymongo and I'm pretty sure my virtualenv is set up correctly.

(django-sample-app)ubuntu@django (884) ~ $ python

Python 2.7.3 (default, Aug  1 2012, 05:14:39) 

[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> import bson

>>> import pymongo

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ubuntu/.virtualenvs/django-sample-app/local/lib/python2.7/site-packages/pymongo/__init__.py", line 80, in <module>
    from pymongo.connection import Connection
  File "/home/ubuntu/.virtualenvs/django-sample-app/local/lib/python2.7/site-packages/pymongo/connection.py", line 39, in <module>
    from pymongo.mongo_client import MongoClient
  File "/home/ubuntu/.virtualenvs/django-sample-app/local/lib/python2.7/site-packages/pymongo/mongo_client.py", line 45, in <module>
    from pymongo import (auth,
  File "/home/ubuntu/.virtualenvs/django-sample-app/local/lib/python2.7/site-packages/pymongo/database.py", line 22, in <module>
    from pymongo.collection import Collection
  File "/home/ubuntu/.virtualenvs/django-sample-app/local/lib/python2.7/site-packages/pymongo/collection.py", line 25, in <module>
    from pymongo.cursor import Cursor
  File "/home/ubuntu/.virtualenvs/django-sample-app/local/lib/python2.7/site-packages/pymongo/cursor.py", line 19, in <module>
    from bson import RE_TYPE
ImportError: cannot import name RE_TYPE
kelorek
  • 6,042
  • 6
  • 29
  • 32
  • Did you install it by `sudo apt-get install python-pymongo`? – 000 Apr 11 '13 at 03:32
  • No, I used pip install pymongo. I just tried installing it with sudo apt-get, and that works, but now i can't import it from the virtualenv – kelorek Apr 11 '13 at 03:59
  • If you run `pip freeze`, is mongo in that list? – 000 Apr 11 '13 at 04:01
  • Um nevermind. I'm an idiot. It works only when installed with apt-get AND pip through the virtualenv. Thanks Joe. And thanks for telling me about pip freeze, that gave me a bunch of free xp. – kelorek Apr 11 '13 at 04:01
  • mongoDB doesn't work with Django. You need to use django-nonrel. Can you syncdb? what verison of django, pymongo, mongodb are you using? – lciamp Apr 13 '13 at 05:05
  • I haven't figured out how to integrate mongo with django yet, I'll look into django-norel. I'm doing it the "stupid way" for now, where I just make inserts manually from the python code by importing pymongo and inserting into the relevant collection based on the URL of the POST request. – kelorek Apr 13 '13 at 06:52
  • mongodb version v2.4.1, pymongo 2.1, and django 1.4.1 – kelorek Apr 13 '13 at 06:53

2 Answers2

19

This error happened to me after pip install (in virtualenv) both pymongo and bson.

Uninstall pymongo and bson and install again just pymongo - it ships with its own version of bson not compatible with the bson package.

https://stackoverflow.com/a/12983651/196206

Community
  • 1
  • 1
Messa
  • 24,321
  • 6
  • 68
  • 92
0

I have solved the problem with uninstall pymongo and again install with the following command: python -m pip install pymongo