25

I'm trying to import json_util in my environment file:

from bson import json_util

I get this import error: no module named bson.

I tried to pip install and uninstall pymongo and bson - but nothing seemed to help. I found out that the bson package is included in pymongo so I installed it explicitly and then I received an EPOCH_AWARE import error.

Currently, only pymongo is installed. It works when I force the virtual environment by using this line:

#subprocess.Popen(['/home/.virtualenvs/simple_worker/bin/python', fileName])

But when I try to run it through the os like this:

os.system('PYTHON_ENV=%s python %s' % (env, fileName))

I get this import error on bson.

Edit: Here is the pip list output

(simple_worker) ➜ worker  pip list                                                                                         ✭ ✱
apache-libcloud (0.17.0)
backports.ssl-match-hostname (3.4.0.2)
gnureadline (6.3.3)
google-api-python-client (1.3.1)
httplib2 (0.9.2)
ipdb (0.8)
ipython (2.3.1)
mock (1.0.1)
oauth2client (1.5.1)
pbr (0.10.7)
pika (0.9.14)
pip (8.0.2)
psutil (2.2.0)
py (1.4.26)
pyaml (15.2.1)
pyasn1 (0.1.7)
pyasn1-modules (0.0.5)
pymongo (3.2.1)
pytest (2.6.4)
pytest-mock (0.4.0)
pytz (2015.4)
PyYAML (3.11)
redis (2.10.3)
requests (2.8.0)
rsa (3.1.4)
setuptools (11.0)
simplejson (3.6.5)
six (1.9.0)
stevedore (1.2.0)
uritemplate (0.6)
urllib3 (1.10)
virtualenv (12.0.7)
virtualenv-clone (0.2.5)
virtualenvwrapper (4.3.2)

Any ideas?

Liad Amsalem
  • 413
  • 1
  • 6
  • 14
  • 3
    Which OS are you running at? Did you try installing `bson` using pip as admin/super user? Are you working with virtualenv's or not? If so, did you activate it properly before running the code? – albert Feb 07 '16 at 15:02
  • `PYTHON_ENV` is not a variable used by Python – Andrea Corbellini Feb 07 '16 at 15:03
  • 1
    what happens if you import bson on repl , what is the output of pip list? – FabioCosta Feb 07 '16 at 15:10
  • @albert I'm runing on ubuntu. I tried installing bson using pip (as admin). I'm working with virtualenv and i activate it before pip install. – Liad Amsalem Feb 07 '16 at 15:29
  • In your terminal try `pip freeze` in order to get a list of all 'pip-installed' packages and have a look whether `pymongo` is installed correctly. – albert Feb 07 '16 at 15:33
  • Running ubuntu, installed with 'sudo pip install bson'. That worked but there's no json_util in it. I take you must use the version of bson in MongoDB to get json_util – Keith Brodie Feb 07 '16 at 15:34
  • @FabioCosta pip list added to the question – Liad Amsalem Feb 07 '16 at 15:37
  • Did you install something `pymongo`-related using `apt`? There might be some incompatibilities: http://stackoverflow.com/a/16004833/3991125 http://www.openjems.com/pymongo-import-problem/ http://api.mongodb.org/python/current/installation.html – albert Feb 07 '16 at 15:44

3 Answers3

49

If you don't get the no module named bson error but the EPOCH_AWARE import error , this is due to a name clash between bson and pymongo . In this case you should do these in order :

sudo pip uninstall bson
sudo pip uninstall pymongo
sudo pip install pymongo

you don't need to install bson again

kommradHomer
  • 4,127
  • 5
  • 51
  • 68
  • tks, this error happened to me the same, supposed to use module pymongo instead of bson – Dee Nov 23 '20 at 09:54
2

Please try on your terminal:

sudo apt-get purge python-bson
sudo apt-get install python-bson

Regards

A. STEFANI
  • 6,707
  • 1
  • 23
  • 48
0

I had used both pymongo and bson in my code, and I've had the same error no module named bson.
I first tried to install bson but it didn't work, so I uninstalled it again, then tried to re-install pymongo and it worked for me. I've used the following commands.
if you've installed bson, uninstall it:

pip uninstall bson

and uninstall then install pymongo again:

pip uninstall pymongo
pip install pymongo