Skip to content

Commit 545e636

Browse files
authored
Consistent messages if imports fail (#1831)
1 parent 4ddb9b1 commit 545e636

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

pymodbus/client/serial.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import asyncio
55
import time
6-
from contextlib import suppress
76
from functools import partial
87
from typing import Any
98

@@ -15,9 +14,13 @@
1514
from pymodbus.utilities import ModbusTransactionState
1615

1716

18-
with suppress(ImportError):
17+
try:
1918
import serial
2019

20+
PYSERIAL_MISSING = False
21+
except ImportError:
22+
PYSERIAL_MISSING = True
23+
2124

2225
class AsyncModbusSerialClient(ModbusBaseClient, asyncio.Protocol):
2326
"""**AsyncModbusSerialClient**.
@@ -74,6 +77,11 @@ def __init__(
7477
**kwargs: Any,
7578
) -> None:
7679
"""Initialize Asyncio Modbus Serial Client."""
80+
if PYSERIAL_MISSING:
81+
raise RuntimeError(
82+
"Serial client requires pyserial "
83+
'Please install with "pip install pyserial" and try again.'
84+
)
7785
asyncio.Protocol.__init__(self)
7886
ModbusBaseClient.__init__(
7987
self,

pymodbus/server/async_io.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
from pymodbus.transport import CommParams, CommType, ModbusProtocol
1818

1919

20-
with suppress(ImportError):
21-
pass
22-
23-
2420
# --------------------------------------------------------------------------- #
2521
# Protocol Handlers
2622
# --------------------------------------------------------------------------- #

pymodbus/server/reactive/main.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414

1515
try:
1616
from aiohttp import web
17+
18+
AIOHTTP_MISSING = False
1719
except ImportError:
18-
print(
19-
"Reactive server requires aiohttp. "
20-
'Please install with "pip install aiohttp" and try again.'
21-
)
22-
sys.exit(1)
20+
AIOHTTP_MISSING = True
2321

2422
from pymodbus import __version__ as pymodbus_version
2523
from pymodbus.datastore import ModbusServerContext, ModbusSlaveContext
@@ -199,6 +197,11 @@ class ReactiveServer:
199197

200198
def __init__(self, host, port, modbus_server):
201199
"""Initialize."""
200+
if AIOHTTP_MISSING:
201+
raise RuntimeError(
202+
"Reactive server requires aiohttp. "
203+
'Please install with "pip install aiohttp" and try again.'
204+
)
202205
self._web_app = web.Application()
203206
self._runner = web.AppRunner(self._web_app)
204207
self._host = host

pymodbus/server/simulator/http_server.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
from typing import List
1010

1111

12-
with contextlib.suppress(ImportError):
12+
try:
1313
from aiohttp import web
1414

15+
AIOHTTP_MISSING = False
16+
except ImportError:
17+
AIOHTTP_MISSING = True
18+
1519
from pymodbus.datastore import ModbusServerContext, ModbusSimulatorContext
1620
from pymodbus.datastore.simulator import Label
1721
from pymodbus.device import ModbusDeviceIdentification
@@ -117,8 +121,11 @@ def __init__(
117121
custom_actions_module: str = None,
118122
):
119123
"""Initialize http interface."""
120-
if not web:
121-
raise RuntimeError("aiohttp not installed!")
124+
if AIOHTTP_MISSING:
125+
raise RuntimeError(
126+
"Simulator server requires aiohttp. "
127+
'Please install with "pip install aiohttp" and try again.'
128+
)
122129
with open(json_file, encoding="utf-8") as file:
123130
setup = json.load(file)
124131

0 commit comments

Comments
 (0)