Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python2: Installing json_util

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?

like image 512
user1581390 Avatar asked Oct 17 '18 15:10

user1581390


2 Answers

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

like image 55
bauman.space Avatar answered Oct 29 '22 04:10

bauman.space


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
like image 37
Rakurai Avatar answered Oct 29 '22 04:10

Rakurai