Repository: pymodbus-dev/pymodbus

Issue:
pymodbus.server requires redis in 3.1.2
<!--

Before opening a new issue, make sure you do the following:
    * check that your issue isn't already filed: https://github.com/pymodbus-dev/pymodbus/issues
    * check the discussions forum https://github.com/pymodbus-dev/pymodbus/discussions
    * prepare a short, runnable example that reproduce the issue with the latest development version of Pymodbus
-->

### Versions

* Python: 3.11
* OS: Linux
* Pymodbus: 3.1.2
* Modbus Hardware (if used):

### Pymodbus Specific
* Server: tcp/rtu/ascii - sync/async
* Client: tcp/rtu/ascii - sync/async

### Description

Running `pymodbus.server --help` raises an exception when `redis` isn't installed.

```
pymodbus.server --help
Traceback (most recent call last):
  File "/home/conda/feedstock_root/build_artifacts/pymodbus_1675064514324/_test_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/bin/pymodbus.server", line 7, in <module>
    from pymodbus.repl.server.main import app
  File "/home/conda/feedstock_root/build_artifacts/pymodbus_1675064514324/_test_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/lib/python3.11/site-packages/pymodbus/repl/server/main.py", line 16, in <module>
    from pymodbus.server.reactive.default_config import DEFAULT_CONFIG
  File "/home/conda/feedstock_root/build_artifacts/pymodbus_1675064514324/_test_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/lib/python3.11/site-packages/pymodbus/server/__init__.py", line 5, in <module>
    from pymodbus.server.async_io import (
  File "/home/conda/feedstock_root/build_artifacts/pymodbus_1675064514324/_test_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/lib/python3.11/site-packages/pymodbus/server/async_io.py", line 12, in <module>
    from pymodbus.datastore import ModbusServerContext
  File "/home/conda/feedstock_root/build_artifacts/pymodbus_1675064514324/_test_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/lib/python3.11/site-packages/pymodbus/datastore/__init__.py", line 3, in <module>
    from pymodbus.datastore.database.redis_datastore import RedisSlaveContext
  File "/home/conda/feedstock_root/build_artifacts/pymodbus_1675064514324/_test_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/lib/python3.11/site-packages/pymodbus/datastore/database/__init__.py", line 2, in <module>
    from pymodbus.datastore.database.redis_datastore import RedisSlaveContext
  File "/home/conda/feedstock_root/build_artifacts/pymodbus_1675064514324/_test_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/lib/python3.11/site-packages/pymodbus/datastore/database/redis_datastore.py", line 5, in <module>
    import redis
ModuleNotFoundError: No module named 'redis'
```

See https://dev.azure.com/conda-forge/feedstock-builds/_build/results?buildId=649693&view=logs&jobId=656edd35-690f-5c53-9ba3-09c10d0bea97&j=656edd35-690f-5c53-9ba3-09c10d0bea97&t=e5c8ab1d-8ff9-5cae-b332-e15ae582ed2d

With 3.1.1, `redis` was an optional dependency.
I'd not expect this to change with a patch release.

Is this a bug that was introduced in 3.1.2?





Discussion:
Same goes for python imports from `pymodbus.datastore`. Both `redis` and `sqlalchemy` are mandatory with 3.1.2
Ups my mistake, I will fix that fast.

Resolving patch:
diff --git a/pymodbus/datastore/database/redis_datastore.py b/pymodbus/datastore/database/redis_datastore.py
--- a/pymodbus/datastore/database/redis_datastore.py
+++ b/pymodbus/datastore/database/redis_datastore.py
@@ -2,7 +2,11 @@
 # pylint: disable=missing-type-doc
 import logging
 
-import redis
+
+try:
+    import redis
+except ImportError:
+    pass
 
 from pymodbus.interfaces import IModbusSlaveContext
 from pymodbus.utilities import pack_bitstring, unpack_bitstring
diff --git a/pymodbus/datastore/database/sql_datastore.py b/pymodbus/datastore/database/sql_datastore.py
--- a/pymodbus/datastore/database/sql_datastore.py
+++ b/pymodbus/datastore/database/sql_datastore.py
@@ -2,11 +2,15 @@
 # pylint: disable=missing-type-doc
 import logging
 
-import sqlalchemy
-import sqlalchemy.types as sqltypes
-from sqlalchemy.schema import UniqueConstraint
-from sqlalchemy.sql import and_
-from sqlalchemy.sql.expression import bindparam
+
+try:
+    import sqlalchemy
+    import sqlalchemy.types as sqltypes
+    from sqlalchemy.schema import UniqueConstraint
+    from sqlalchemy.sql import and_
+    from sqlalchemy.sql.expression import bindparam
+except ImportError:
+    pass
 
 from pymodbus.interfaces import IModbusSlaveContext
 


Test patch: