From b67ce9050dddcf3439ccdf58be93b0e4deb50c12 Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Mon, 16 Oct 2023 20:39:06 -0500 Subject: [PATCH] Consistent messages if imports fail --- pymodbus/client/serial.py | 12 ++++++++++-- pymodbus/server/async_io.py | 4 ---- pymodbus/server/reactive/main.py | 13 ++++++++----- pymodbus/server/simulator/http_server.py | 13 ++++++++++--- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/pymodbus/client/serial.py b/pymodbus/client/serial.py index 3321584fb..a32636082 100644 --- a/pymodbus/client/serial.py +++ b/pymodbus/client/serial.py @@ -3,7 +3,6 @@ import asyncio import time -from contextlib import suppress from functools import partial from typing import Any @@ -15,9 +14,13 @@ from pymodbus.utilities import ModbusTransactionState -with suppress(ImportError): +try: import serial + PYSERIAL_MISSING = False +except ImportError: + PYSERIAL_MISSING = True + class AsyncModbusSerialClient(ModbusBaseClient, asyncio.Protocol): """**AsyncModbusSerialClient**. @@ -74,6 +77,11 @@ def __init__( **kwargs: Any, ) -> None: """Initialize Asyncio Modbus Serial Client.""" + if PYSERIAL_MISSING: + raise RuntimeError( + "Serial client requires pyserial " + 'Please install with "pip install pyserial" and try again.' + ) asyncio.Protocol.__init__(self) ModbusBaseClient.__init__( self, diff --git a/pymodbus/server/async_io.py b/pymodbus/server/async_io.py index a5bcb558b..22f241abd 100644 --- a/pymodbus/server/async_io.py +++ b/pymodbus/server/async_io.py @@ -17,10 +17,6 @@ from pymodbus.transport import CommParams, CommType, ModbusProtocol -with suppress(ImportError): - pass - - # --------------------------------------------------------------------------- # # Protocol Handlers # --------------------------------------------------------------------------- # diff --git a/pymodbus/server/reactive/main.py b/pymodbus/server/reactive/main.py index 409cb8ae9..511ae7dca 100644 --- a/pymodbus/server/reactive/main.py +++ b/pymodbus/server/reactive/main.py @@ -14,12 +14,10 @@ try: from aiohttp import web + + AIOHTTP_MISSING = False except ImportError: - print( - "Reactive server requires aiohttp. " - 'Please install with "pip install aiohttp" and try again.' - ) - sys.exit(1) + AIOHTTP_MISSING = True from pymodbus import __version__ as pymodbus_version from pymodbus.datastore import ModbusServerContext, ModbusSlaveContext @@ -199,6 +197,11 @@ class ReactiveServer: def __init__(self, host, port, modbus_server): """Initialize.""" + if AIOHTTP_MISSING: + raise RuntimeError( + "Reactive server requires aiohttp. " + 'Please install with "pip install aiohttp" and try again.' + ) self._web_app = web.Application() self._runner = web.AppRunner(self._web_app) self._host = host diff --git a/pymodbus/server/simulator/http_server.py b/pymodbus/server/simulator/http_server.py index 4ae7c40d1..e3c7468f3 100644 --- a/pymodbus/server/simulator/http_server.py +++ b/pymodbus/server/simulator/http_server.py @@ -9,9 +9,13 @@ from typing import List -with contextlib.suppress(ImportError): +try: from aiohttp import web + AIOHTTP_MISSING = False +except ImportError: + AIOHTTP_MISSING = True + from pymodbus.datastore import ModbusServerContext, ModbusSimulatorContext from pymodbus.datastore.simulator import Label from pymodbus.device import ModbusDeviceIdentification @@ -117,8 +121,11 @@ def __init__( custom_actions_module: str = None, ): """Initialize http interface.""" - if not web: - raise RuntimeError("aiohttp not installed!") + if AIOHTTP_MISSING: + raise RuntimeError( + "Simulator server requires aiohttp. " + 'Please install with "pip install aiohttp" and try again.' + ) with open(json_file, encoding="utf-8") as file: setup = json.load(file)