Skip to content

Commit 1da18df

Browse files
authored
Merge pull request #1663 from zm711/clean-up-import
Cleanup import to use `importlib.util.find_spec`
2 parents 976c4aa + 57b6c3f commit 1da18df

File tree

4 files changed

+18
-25
lines changed

4 files changed

+18
-25
lines changed

neo/io/__init__.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -280,24 +280,17 @@
280280

281281
import pathlib
282282
from collections import Counter
283+
import importlib.util
283284

284-
# try to import the neuroshare library.
285+
# check if neuroshare library exists
285286
# if it is present, use the neuroshareapiio to load neuroshare files
286287
# if it is not present, use the neurosharectypesio to load files
287-
try:
288-
import neuroshare as ns
289-
except ModuleNotFoundError as err:
290-
from neo.io.neurosharectypesio import NeurosharectypesIO as NeuroshareIO
291-
292-
# print("\n neuroshare library not found, loading data with ctypes" )
293-
# print("\n to use the API be sure to install the library found at:")
294-
# print("\n www.http://pythonhosted.org/neuroshare/")
295288

296-
else:
289+
neuroshare_spec = importlib.util.find_spec("neuroshare")
290+
if neuroshare_spec is not None:
297291
from neo.io.neuroshareapiio import NeuroshareapiIO as NeuroshareIO
298-
299-
# print("neuroshare library successfully imported")
300-
# print("\n loading with API...")
292+
else:
293+
from neo.io.neurosharectypesio import NeurosharectypesIO as NeuroshareIO
301294

302295
from neo.io.alphaomegaio import AlphaOmegaIO
303296
from neo.io.asciiimageio import AsciiImageIO

neo/io/baseio.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@
1313

1414
from __future__ import annotations
1515
from pathlib import Path
16-
17-
try:
18-
from collections.abc import Sequence
19-
except ImportError:
20-
from collections import Sequence
16+
from collections.abc import Sequence
2117
import logging
2218

2319
from neo import logging_handler

neo/io/nixio.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
from uuid import uuid4
2828
import warnings
2929
from packaging.version import Version
30+
import importlib.util
31+
import importlib.metadata
3032

3133
import quantities as pq
3234
import numpy as np
@@ -122,17 +124,15 @@ def dt_from_nix(nixdt, annotype):
122124

123125

124126
def check_nix_version():
125-
try:
126-
import nixio
127-
except ImportError:
128-
raise Exception(
127+
nixio_spec = importlib.util.find_spec("nixio")
128+
if nixio_spec is None:
129+
raise ImportError(
129130
"Failed to import NIX. "
130131
"The NixIO requires the Python package for NIX "
131132
"(nixio on PyPi). Try `pip install nixio`."
132133
)
133134

134-
# nixio version numbers have a 'v' prefix which breaks the comparison
135-
nixverstr = nixio.__version__.lstrip("v")
135+
nixverstr = importlib.metadata.version("nixio")
136136
try:
137137
nixver = Version(nixverstr)
138138
except ValueError:

neo/io/stimfitio.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
08 Feb 2014, C. Schmidt-Hieber, University College London
2727
"""
2828

29+
import importlib.util
30+
2931
import numpy as np
3032
import quantities as pq
3133

@@ -92,7 +94,9 @@ def __init__(self, filename=None):
9294
"""
9395
# We need this module, so try importing now so that it fails on
9496
# instantiation rather than read_block
95-
import stfio # noqa
97+
stfio_spec = importlib.util.find_spec("stfio")
98+
if stfio_spec is None:
99+
raise ImportError("stfio must be installed to use StimfitIO")
96100

97101
BaseIO.__init__(self)
98102

0 commit comments

Comments
 (0)