MCPcopy Create free account
hub / github.com/ipython/ipython / try_import

Function try_import

IPython/core/completerlib.py:158–180  ·  view source on GitHub ↗

Try to import given module and return list of potential completions.

(mod: str, only_modules=False)

Source from the content-addressed store, hash-verified

156
157
158def try_import(mod: str, only_modules=False) -> List[str]:
159 """
160 Try to import given module and return list of potential completions.
161 """
162 mod = mod.rstrip('.')
163 try:
164 m = import_module(mod)
165 except:
166 return []
167
168 m_is_init = '__init__' in (getattr(m, '__file__', '') or '')
169
170 completions = []
171 if (not hasattr(m, '__file__')) or (not only_modules) or m_is_init:
172 completions.extend( [attr for attr in dir(m) if
173 is_importable(m, attr, only_modules)])
174
175 completions.extend(getattr(m, '__all__', []))
176 if m_is_init:
177 completions.extend(module_list(os.path.dirname(m.__file__)))
178 completions_set = {c for c in completions if isinstance(c, str)}
179 completions_set.discard('__init__')
180 return list(completions_set)
181
182
183#-----------------------------------------------------------------------------

Callers 3

test_tryimportMethod · 0.90
test_module_without_initFunction · 0.90
module_completionFunction · 0.85

Calls 2

is_importableFunction · 0.85
module_listFunction · 0.85

Tested by 2

test_tryimportMethod · 0.72
test_module_without_initFunction · 0.72