+++
EDIT: Long time after putting the question online I noticed that this is a spin-off of Can't connect to MongoDB 2.0.5 database with pymongo 2.2 which says that you have to install bson before you install pymongo. I am asking here not for this already known solution, but for the reason of this needed install order. And I add a small thing, I am installing "bson" module as "pybson", which makes it possible to distinguish the import bson
name clash of the two packages.
+++
I am using a workaround to avoid the name clash of pymongo's bson module and bson's (py-bson on GitHub) bson module: I am installing bson package as pybson, see https://github.com/py-bson/bson/issues/70.
From the answer at pip install of eve package installs bson and pymongo which breaks pymongo, we get the main idea:
pymongo
doesn't bringbson
as a dependency, it just has its ownbson
implementation. The problem ispymongo
installs itsbson
as a top-level directory insite-packages/
thus overwriting any existingbson
there.
But this does not explain why the install order [1. bson, 2. pymongo] solves the issue, instead you would expect it to be exactly the other way round!
In my case, I have installed a new system, using anaconda as the base. I had installed bson using pip install pybson
, and it said:
Traceback (most recent call last):
File "", line 1, in import pybson # same as bson
File "C:\Users\Admin\anaconda3\lib\site-packages\pybson_init_.py", line 23, in from .objectid import ObjectId
File "C:\Users\Admin\anaconda3\lib\site-packages\pybson\objectid.py", line 30, in from bson.py3compat import PY3, bytes_from_hex, string_type, text_type
ModuleNotFoundError: No module named 'bson'
After installing pymongo in addition to pybson, using conda install pymongo
, the import pybson
statement worked. Why?
The quoted idea of the question must be put the other way round: pip install bson
after pip install pymongo
disturbs pymongo's bson dependency so that pymongo's bson module will not work anymore. It is probably not because bson overwrites pymongo's bson dependency, but rather because of the name clash: both packages use the same bson module 'bson', and that causes the same effect as if bson overwrote pymongo's bson.
Strange enough: you have to install pymongo in addition to (py)bson, as the question states. That hints at bson using the bson dependency of pymongo in its own package.
As installing bson with pip install bson
after pip install pymongo
will disturb pymongo's bson, see Can't connect to MongoDB 2.0.5 database with pymongo 2.2, we can assume that there is a name clash between the two bson modules which is dominated by the one that is installed as the last. It seems as if the (py)bson package needs a dominating pymongo bson dependency in addition to its own bson module, and (py)bson imports the pymongo dependency as import bson
in its internal scripts, even though it has the bson module itself. I do not know whether the domination is caused by overwriting, or whether this is just a problem of the python environment. The former is more likely, since the order of installs (first pip install bson
, afterwards pip install pymongo
), becomes irrelevant as soon as you install bson with pip install pybson
instead of pip install bson
, see pip install of eve package installs bson and pymongo which breaks pymongo.