LSST Applications  21.0.0-142-gef555c1e+42c9bccae2,22.0.0+052faf71bd,22.0.0+1c4650f311,22.0.0+40ce427c77,22.0.0+5b6c068b1a,22.0.0+7589c3a021,22.0.0+81ed51be6d,22.0.1-1-g7d6de66+6cae67f2c6,22.0.1-1-g87000a6+314cd8b7ea,22.0.1-1-g8760c09+052faf71bd,22.0.1-1-g8e32f31+5b6c068b1a,22.0.1-10-g779eefa+a163f08322,22.0.1-12-g3bd7ecb+bbeacc25a9,22.0.1-15-g63cc0c1+2a7037787d,22.0.1-17-ge5a99e88+3d2c1afe2e,22.0.1-19-g88addfe+6cae67f2c6,22.0.1-2-g1cb3e5b+84de06d286,22.0.1-2-g8ef0a89+6cae67f2c6,22.0.1-2-g92698f7+1c4650f311,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gb66926d+5b6c068b1a,22.0.1-2-gcb770ba+0723a13595,22.0.1-2-ge470956+ff9f1dc8d5,22.0.1-22-g608e23ac+2ac85e833c,22.0.1-29-g184b6e44e+8b185d4e2d,22.0.1-3-g59f966b+11ba4df19d,22.0.1-3-g8c1d971+f90df4c6d0,22.0.1-3-g997b569+d69a7aa2f8,22.0.1-3-gaaec9c0+4d194bf81c,22.0.1-4-g1930a60+283d9d2f1a,22.0.1-4-g5b7b756+c1283a92b8,22.0.1-4-g8623105+6cae67f2c6,22.0.1-7-gba73697+283d9d2f1a,22.0.1-8-g47d23f5+43acea82f3,master-g5f2689bdc5+40ce427c77,w.2021.38
LSST Data Management Base Package
Functions
lsst.utils.doImport Namespace Reference

Functions

def doImport (importable)
 

Function Documentation

◆ doImport()

def lsst.utils.doImport.doImport (   importable)
Import a python object given an importable string and return it.

Parameters
----------
importable : `str`
    String containing dot-separated path of a Python class, module,
    or member function.

Returns
-------
type : `type`
    Type object. Either a module or class or a function.

Raises
------
TypeError
    ``importable`` is not a `str`.
ModuleNotFoundError
    No module in the supplied import string could be found.
ImportError
    ``importable`` is found but can not be imported or the requested
    item could not be retrieved from the imported module.

Definition at line 28 of file doImport.py.

28 def doImport(importable):
29  """Import a python object given an importable string and return it.
30 
31  Parameters
32  ----------
33  importable : `str`
34  String containing dot-separated path of a Python class, module,
35  or member function.
36 
37  Returns
38  -------
39  type : `type`
40  Type object. Either a module or class or a function.
41 
42  Raises
43  ------
44  TypeError
45  ``importable`` is not a `str`.
46  ModuleNotFoundError
47  No module in the supplied import string could be found.
48  ImportError
49  ``importable`` is found but can not be imported or the requested
50  item could not be retrieved from the imported module.
51  """
52  if not isinstance(importable, str):
53  raise TypeError(f"Unhandled type of importable, val: {importable}")
54 
55  def tryImport(module, fromlist, previousError):
56  pytype = importlib.import_module(module)
57  # Can have functions inside classes inside modules
58  for f in fromlist:
59  try:
60  pytype = getattr(pytype, f)
61  except AttributeError:
62  extra = f"({previousError})" if previousError is not None else ""
63  raise ImportError(f"Could not get attribute '{f}' from '{module}' {extra}")
64  return pytype
65 
66  # Go through the import path attempting to load the module
67  # and retrieve the class or function as an attribute. Shift components
68  # from the module list to the attribute list until something works.
69  moduleComponents = importable.split(".")
70  infileComponents = []
71  previousError = None
72 
73  while moduleComponents:
74  try:
75  pytype = tryImport(".".join(moduleComponents), infileComponents, previousError)
76  if not infileComponents and hasattr(pytype, moduleComponents[-1]):
77  # This module has an attribute with the same name as the
78  # module itself (like doImport.doImport, actually!).
79  # If that attribute was lifted to the package, we should
80  # return the attribute, not the module.
81  try:
82  return tryImport(".".join(moduleComponents[:-1]), moduleComponents[-1:], previousError)
83  except ModuleNotFoundError:
84  pass
85  return pytype
86  except ModuleNotFoundError as e:
87  previousError = str(e)
88  # Move element from module to file and try again
89  infileComponents.insert(0, moduleComponents.pop())
90 
91  raise ModuleNotFoundError(f"Unable to import {importable}")
def doImport(importable)
Definition: doImport.py:28