Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def import_(modname, *args):
if len(args) == 0:
try:
return __import__(modname)
except ImportError as e:
return MissingImport(modname, e)
mods = []
for arg in args:
try:
mod = __import__(modname, globals(), locals(), [arg])
except ImportError as e:
mods.append(MissingImport(modname + '.' + arg, e))
else:
try:
attr = getattr(mod, arg)
except AttributeError as e:
mods.append(MissingImport(modname + '.' + arg, e))
else:
mods.append(attr)
return mods if len(args) > 1 else mods[0]
def import_(modname, *args):
if len(args) == 0:
try:
return __import__(modname)
except ImportError as e:
return MissingImport(modname, e)
mods = []
for arg in args:
try:
mod = __import__(modname, globals(), locals(), [arg])
except ImportError as e:
mods.append(MissingImport(modname + '.' + arg, e))
else:
try:
attr = getattr(mod, arg)
except AttributeError as e:
mods.append(MissingImport(modname + '.' + arg, e))
else:
mods.append(attr)
return mods if len(args) > 1 else mods[0]