0

I have a project using python eve, but lately I have been running into an infuriating dependency problem with pip.

I use a requirements.txt file to install the dependencies in a docker container

however installing the Eve package installs pymongo as a depdency. pymongo depends on a bson module, but not the pypi bson module. However, pip install the pypi bson module which will not work with pymongo, leading to an error of:

from bson.py3compat import abc, string_type, PY3, text_type
ImportError: cannot import name 'abc'

when pymongo is imported.

I have to adjust my dockerfile to do something like this:

RUN pip3 install -r requirements.txt
RUN pip3 uninstall bson --yes
RUN pip3 uninstall pymongo --yes
RUN pip3 install pymongo --user

Is there a way to indicate in a pipefile or a requirements.txt that it needs to not attempt to install bson from pypi?

mstorkson
  • 1,130
  • 1
  • 10
  • 26

3 Answers3

1

pymongo doesn't bring bson as a dependency, it just has its own bson implementation. The problem is pymongo installs its bson as a top-level directory in site-packages/ thus overwriting any existing bson there.

There is no easy way to work around this. Try to contact pymongo authors and persuade them to stop overwriting top-level bson.

phd
  • 82,685
  • 13
  • 120
  • 165
1

Uninstall bson and pymongo. Install the original bson as "pybson" instead, using pip install pybson, then you can have both in parallel. I have asked this name change from the (py)bson project, see https://github.com/py-bson/bson/issues/70. It is accidentally more or less the opposite of what @phd had suggested to ask from pymongo. The (py)bson project member did this "in a minute".

I know that you have to install pymongo afterwards to make import pybson work, see Why do I have to install pymongo after pybson (=bson, GitHub:py-bson) to get pybson imported successfully?. The same accounts when installing bson with the original pip install bson instead of pip install pybson, then you have to keep the order: 1. bson, 2. pymongo, see Can't connect to MongoDB 2.0.5 database with pymongo 2.2.

I have not tested whether the trick of pip install pybson instead of pip install bson makes it irrelevant in which order you install, 1. pybson and 2. pymongo or vice versa, but it is likely so that the order is not relevant then.

On the other hand, generally keeping to this order of installs (1. (py)bson and 2. pymongo) does not harm either, so why not doing so. Both the install as pybson and the right install order may solve the issue, so why not do both.

If you install bson as pybson, you have the option of using the two different bson modules of both packages in a single script. That is why I recommend:

  • first pip install pybson,

  • afterwards pip install pymongo.

I checked whether this exact order is necessary when you install bson package with pip install pybson (not bson!). After installing pymongo, I installed pybson. It does not harm the pymongo installation, and import pybson works as well. From this we can see that the order of install is not important anymore when using the pybson trick.

questionto42
  • 7,175
  • 4
  • 57
  • 90
0

actually, just pip install bson first, then pip install pymongo. this worked for me.

Ahmed Bargady
  • 269
  • 4
  • 6