Skip to content

Commit 9524eed

Browse files
committed
deviceserver: replace use of imp (deprecated) with importlib.
The imp package is deprecated since Python 3.4. Since we are now dependent on at least Python 3.5, we can replace it with importlib. In addition to stop using deprecated functionality, this change means that reading the config no longer injects modules into sys.modules (imp.load_source caused a microscope.config module to be added to sys.modules).
1 parent a16bef0 commit 9524eed

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

microscope/deviceserver.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"""
2525

2626
from collections.abc import Iterable
27-
import imp # this has been deprecated, we should be using importlib
27+
import importlib.util
2828
import logging
2929
import multiprocessing
3030
import signal
@@ -352,8 +352,15 @@ def __main__():
352352
__console__()
353353

354354

355+
def _load_source(filepath):
356+
spec = importlib.util.spec_from_file_location('config', filepath)
357+
module = importlib.util.module_from_spec(spec)
358+
spec.loader.exec_module(module)
359+
return module
360+
361+
355362
def validate_devices(configfile):
356-
config = imp.load_source('microscope.config', configfile)
363+
config = _load_source(configfile)
357364
devices = getattr(config, 'DEVICES', None)
358365
if not devices:
359366
raise Exception("No 'DEVICES=...' in config file.")

0 commit comments

Comments
 (0)