MCPcopy
hub / github.com/mne-tools/mne-python / _soft_import

Function _soft_import

mne/utils/check.py:387–437  ·  view source on GitHub ↗

Import soft dependencies, providing informative errors on failure. Parameters ---------- name : str Name of the module to be imported. For example, 'pandas'. purpose : str A very brief statement (formulated as a noun phrase) explaining what functionality the

(name, purpose, strict=True, *, min_version=None)

Source from the content-addressed store, hash-verified

385
386
387def _soft_import(name, purpose, strict=True, *, min_version=None):
388 """Import soft dependencies, providing informative errors on failure.
389
390 Parameters
391 ----------
392 name : str
393 Name of the module to be imported. For example, 'pandas'.
394 purpose : str
395 A very brief statement (formulated as a noun phrase) explaining what
396 functionality the package provides to MNE-Python.
397 strict : bool
398 Whether to raise an error if module import fails.
399 """
400 # Mapping import namespaces to their pypi package name
401 pip_name = dict(
402 sklearn="scikit-learn",
403 mne_bids="mne-bids",
404 mne_nirs="mne-nirs",
405 mne_features="mne-features",
406 mne_qt_browser="mne-qt-browser",
407 mne_connectivity="mne-connectivity",
408 mne_gui_addons="mne-gui-addons",
409 pyvista="pyvistaqt",
410 hed="hedtools",
411 ).get(name, name)
412
413 got_version = None
414 try:
415 mod = import_module(name)
416 except (ImportError, ModuleNotFoundError):
417 mod = False
418 else:
419 have, got_version = check_version(
420 name,
421 min_version=min_version,
422 return_version=True,
423 )
424 if not have:
425 mod = False
426 if mod is False and strict:
427 extra = "" if min_version is None else f">={min_version}"
428 if got_version is not None:
429 extra += f" (found version {got_version})"
430 raise RuntimeError(
431 f"For {purpose} to work, the module {name}{extra} is needed, "
432 "but it could not be imported. Use the following installation method "
433 "appropriate for your environment:\n\n"
434 f" pip install {pip_name}\n"
435 f" conda install -c conda-forge {pip_name}"
436 )
437 return mod
438
439
440def _check_pandas_installed(strict=True):

Callers 15

test_soft_importFunction · 0.90
__init__Method · 0.85
get_meg_helmet_surfFunction · 0.85
_open_lockFunction · 0.85
_import_h5io_funcsFunction · 0.85
_check_pandas_installedFunction · 0.85
_check_edfio_installedFunction · 0.85
_check_pybv_installedFunction · 0.85
find_bad_channels_lofFunction · 0.85

Calls 1

check_versionFunction · 0.85

Tested by 1

test_soft_importFunction · 0.72