long2ice / meilisync

Realtime sync data from MySQL/PostgreSQL/MongoDB to Meilisearch
https://github.com/long2ice/meilisync
Apache License 2.0
285 stars 43 forks source link

Gracefully handle optional dependencies #98

Open MattExact opened 6 months ago

MattExact commented 6 months ago

If you have not installed via meilisync[all], importlib.import_module will throw a ModuleNotFoundError for any missing dependencies. This kind of defeats the point of supporting meilisync[postgres] etc.

If the exception is caught it will support the use of optional dependencies.

example error:

  File ".venv/lib/python3.12/site-packages/meilisync/discover.py", line 23, in <module>
    _sources = _discover(source, Source)
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/meilisync/discover.py", line 16, in _discover
    mod = importlib.import_module(f"{module.__name__}.{m.name}")
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/meilisync/source/mongo.py", line 3, in <module>
    import motor.motor_asyncio
ModuleNotFoundError: No module named 'motor'