pythonmongodbpymongomongoenginebeanie

Pymongo and Beanie Incompatibility issues


I'm trying to migrate from motor to pymongo's AsyncMongoClient.

After doing some upgrading/installing on pymongo I am having the below error when running this import from beanie import Document

ImportError: cannot import name '_QUERY_OPTIONS' from 'pymongo.cursor'

Using python 3.11.9

My dependencies are below:

dependencies = [
    "fastapi==0.95.0",
    "uvicorn==0.22.0",
    "gunicorn==20.1.0",
    "elastic-apm==6.15.1",
    "pymongo==4.13.2",
    "pydantic==1.10.18",
    "beanie==1.29.0",
    "dnspython==2.2.1",
    "python-dotenv==1.0.0",
    "psutil==5.9.4",
    "loguru==0.6.0",
    "fastapi-etag==0.4.0",
    "mongoengine==0.29.1",
    "elasticsearch7==7.17.12",
    "elasticsearch-dsl==7.4.1",
    "promise==2.3",
    "requests==2.31.0",
    "pytz==2023.3",
    "singleton-decorator==1.0.0",
    "cachetools==5.3.1",
    "pymysql==1.0.2",
    "requests-custom-header==0.1.1",
    "aiohttp==3.9.1",
    "telempack==1.7.11",
    "polars==1.9.0",
    "jinja2==3.1.3",
    "oracledb==2.5.1",
    "numpy==2.2.3",
    "pika==1.3.2",
    "zstandard==0.23.0",
]

I've tried multiple attempts to upgrade/downgrade beanie/pymongo/mongoengine but it keeps throwing this error. Any ideas?


Solution

  • This seems to be an issue with using pymongo >= 4.9 with older versions of libraries that haven't been updated to be compatible with the breaking changes introduced in those pymongo releases.

    This is because the _QUERY_OPTIONS attribute was removed from pymongo.cursor in 4.9 and above. Libraries that rely on this attribute will break if they haven't been updated to accommodate this change. Therefore, the solution involves either updating the dependent library or downgrading pymongo to < 4.9. See this GitHub issue.