9

I used to import json_util from bson:

from bson import json_util

Now I get:

ImportError: cannot import name json_util

How can I install json_util now?

user1581390
  • 1,900
  • 5
  • 25
  • 38

2 Answers2

10

Did you do?

pip install bson

which isntalls this 3rd party package which does not include all the goodies found in MongoDB's package

https://pypi.org/project/bson/

json_util (and a host of other utils) are provided in MongoDB Inc's pymongo package.

pip install pymongo

https://pypi.org/project/pymongo/

As noted on the pymongo pypi page

Do not install the “bson” package from pypi. PyMongo comes with its own bson package; doing “easy_install bson” installs a third-party package that is incompatible with PyMongo.

some distros package MongoDB's bson package You might be on RHEL derivative since you're looking at py27. EPEL has a slightly out of date version you can install with

yum install python-bson

http://fedora-epel.mirrors.tds.net/fedora-epel/7/x86_64/Packages/p/python-bson-2.5.2-4.el7.x86_64.rpm

mainline ubuntu also packages it (and also separates the C module into the -ext package)

https://packages.ubuntu.com/bionic/python-bson

apt-get install python-bson python-bson-ext

bauman.space
  • 1,993
  • 13
  • 15
3

As noted in this issue and explained in the detailed answer, the quick fix is to

pip uninstall bson
pip uninstall pymongo
pip install pymongo
Rakurai
  • 974
  • 7
  • 15