Pymongo ModuleNotFoundError: No module named 'bson'

Initially I was having issues with pymongo because of a conflict between the bson module on PyPI and the one that comes with pymongo. I addressed this issue by doing the following:

pip uninstall bson
pip uninstall pymongo
pip install pymongo

Everything is successful, no errors here.

However, now I have a new issue:

  File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\__init__.py", line 87, in <module>
    from pymongo import _csot
  File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\_csot.py", line 22, in <module>
    from pymongo.write_concern import WriteConcern
  File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\write_concern.py", line 19, in <module>
    from pymongo.errors import ConfigurationError
  File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\errors.py", line 18, in <module>
    from bson.errors import InvalidDocument
ModuleNotFoundError: No module named 'bson'

I’ve spent hours trying to figure this out, any help would be greatly appreciated.

I have the same problem. It was working in python 3.8 and it broke then I upgraded to python 3.10.6
bson==0.5.10
pymongo==4.3.3

I’ve tried reinstalling the packages and downgrading pip to 22.3.1

I managed to fix it. I’m not sure what I did but I installed a new pyenv environment and I started using GitHub - pyenv/pyenv-virtualenv: a pyenv plugin to manage virtualenv (a.k.a. python-virtualenv) -

Do not install the “bson” package from pypi. See the warning here: Installing / Upgrading — PyMongo 4.3.3 documentation

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

1 Like

I install dependencies from a requirements file. bson is not listed there. These are our pymongo requirements:

pymongo==4.3.3
pymongo[zstd]
pymongo[srv]

I got it working after lots of trial and error but I’m not sure what I did to fix it.

@shane 's answer is clear about the package. however, I want to add some other things.

new versions of some libraries versus the version of python installation has always its problems. a new library version may not have compiled binaries for old python, or a new/old library may not be compiled yet for new python. In such cases, the installer tries to use a system C compiler (CPython at least) and compile the library for the python of the currently active environment.

emphasis is on the “version” and “active”. You guys might be forgetting to change activate/switch environments at times, which gives different results every time you try.

Or maybe, as the lines get longer, you might be ignoring/missing possible compile errors on the way. so the actual library you were expecting might not even be there. This is very common in windows systems as compilers are not easy to install/manage.

Two things are important when you start having problems: make sure you are in the right environment, and check the installation result. A new/clear virtual environment with a suitable python version is mostly the best solution.

PS: pymongo 4.3.3, currently latest, has wheels for python 3.7 and later for most distributions, and a compile should be triggered for previous python versions, whereas 4.2.0 does not have any for python 3.11 which also should trigger a compile.

This post was flagged by the community and is temporarily hidden.