Search code examples
pythonpymongopypi

How is it possible for bson to be imported without the pymongo namespace?


The Pypi entry for pymongo entry states[1]

The bson package is an implementation of the BSON format for Python

and

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

But the pymongo API documentation[2] shows examples of bson being imported without a pymongo prefix / namespace like so:

import bson

How is it possible for bson to be imported without the pymongo namespace?

I would have expected the import statement to be import pymongo.bson because the referenced bson implementation came with pymongo.

[1] https://pypi.python.org/pypi/pymongo/

[2] http://api.mongodb.org/python/3.0.3/api/bson/index.html


Solution

  • Because pymongo setup installs the bson package it provides the same way it installs the pymongo package it also provides.

    Look at github where you can see the two modules: pymongo and bson: https://github.com/mongodb/mongo-python-driver

    The setup.py file defines:

    "packages": ["bson", "pymongo", "gridfs"]
    

    So these are the packages installed in your python site-packages/ dir