LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
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