|
11 | 11 | """ |
12 | 12 |
|
13 | 13 | from importlib import import_module |
| 14 | +from importlib.util import find_spec |
14 | 15 |
|
15 | 16 |
|
16 | 17 | class VISAInstrument: |
17 | 18 | def __init__(self, device_identifier, backend, timeout): |
18 | | - try: |
19 | | - _py_pyvisa_module = import_module('pyvisa') |
20 | | - except ModuleNotFoundError as e: |
21 | | - raise ModuleNotFoundError("pyvisa module not found, please install it") from e |
22 | | - else: |
23 | | - _pyvisa_resource_manager = _py_pyvisa_module.ResourceManager(backend) |
24 | | - if _pyvisa_resource_manager is None: |
25 | | - raise ValueError("pyVISA backend not found") |
26 | | - self._pyvisa_device = _pyvisa_resource_manager.open_resource(device_identifier) |
27 | | - if 'SOCKET' in device_identifier: |
28 | | - self._pyvisa_device.read_termination = '\n' |
29 | | - self._pyvisa_device.write_termination = '\n' |
30 | | - if self._pyvisa_device is None: |
31 | | - raise ValueError("pyVISA device not found") |
| 19 | + if not find_spec("pyvisa"): |
| 20 | + raise ModuleNotFoundError("pyvisa module not found, please install it") |
| 21 | + |
| 22 | + _py_pyvisa_module = import_module('pyvisa') |
| 23 | + _pyvisa_resource_manager = _py_pyvisa_module.ResourceManager(backend) |
| 24 | + if _pyvisa_resource_manager is None: |
| 25 | + raise ValueError("pyVISA backend not found") |
| 26 | + self._pyvisa_device = _pyvisa_resource_manager.open_resource(device_identifier) |
| 27 | + if self._pyvisa_device is None: |
| 28 | + raise ValueError("pyVISA device not found") |
32 | 29 | try: |
33 | 30 | self._pyvisa_device.clear() |
34 | 31 | except _py_pyvisa_module.errors.VisaIOError as e: |
|
0 commit comments