MCPcopy
hub / github.com/pallets/jinja / import_string

Function import_string

src/jinja2/utils.py:140–161  ·  view source on GitHub ↗

Imports an object based on a string. This is useful if you want to use import paths as endpoints or something similar. An import path can be specified either in dotted notation (``xml.sax.saxutils.escape``) or with a colon as object delimiter (``xml.sax.saxutils:escape``). If the

(import_name: str, silent: bool = False)

Source from the content-addressed store, hash-verified

138
139
140def import_string(import_name: str, silent: bool = False) -> t.Any:
141 """Imports an object based on a string. This is useful if you want to
142 use import paths as endpoints or something similar. An import path can
143 be specified either in dotted notation (``xml.sax.saxutils.escape``)
144 or with a colon as object delimiter (``xml.sax.saxutils:escape``).
145
146 If the `silent` is True the return value will be `None` if the import
147 fails.
148
149 :return: imported object
150 """
151 try:
152 if ":" in import_name:
153 module, obj = import_name.split(":", 1)
154 elif "." in import_name:
155 module, _, obj = import_name.rpartition(".")
156 else:
157 return __import__(import_name)
158 return getattr(__import__(module, None, None, [obj]), obj)
159 except (ImportError, AttributeError):
160 if not silent:
161 raise
162
163
164def open_if_exists(filename: str, mode: str = "rb") -> t.Optional[t.IO[t.Any]]:

Callers 2

babel_extractFunction · 0.85
load_extensionsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected