10 examples of 'python import alias' in Python

Every line of 'python import alias' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
62def import_or_alias(python_path: str) -> Union[Callable, str]:
63 """Import an object from a full python path or assume it is an alias for some object
64 in TensorFlow (e.g. "categorical_crossentropy").
65
66 Args:
67 python_path: str, full python path or alias.
68
69 Returns:
70 callable/str, import object/alias.
71
72 Raises:
73 ImportError, object not found.
74 """
75 python_path = _maybe_fix_tensorflow_python_path(python_path)
76 try:
77 module_path, obj_name = python_path.rsplit(".", 1)
78 except ValueError:
79 return python_path
80
81 module = importlib.import_module(module_path)
82
83 if not hasattr(module, obj_name):
84 raise ImportError(f"object: {obj_name} was not found in module {module_path}")
85 return getattr(module, obj_name)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

619def import_get_module_object(node, asname, context_file=None):
620 """given a name which has been introduced by this statement,
621 return the name of the module and the name of the object in the module
622
623 if the name is a module, object will be None
624 """
625 return node.get_real_name(asname), None
22def visit_Import(self, node):
23 """
24 This executes every time an "import foo" style import statement
25 is parsed.
26 """
27 for n in node.names:
28 self.imports.append((n.name, n.asname, None))
90def __import__(name, globals=None, locals=None, fromlist=None):
91 """An alternative to the import function so that we can import
92 modules defined as strings.
93
94 This code was taken from: http://docs.python.org/lib/examples-imp.html
95 """
96 # Fast path: see if the module has already been imported.
97 try:
98 return sys.modules[name]
99 except KeyError:
100 pass
101
102 # If any of the following calls raises an exception,
103 # there's a problem we can't handle -- let the caller handle it.
104 module_name = name.split('.')[-1]
105 module_path = os.path.join(EXAMPLE_DIR, *name.split('.')[:-1])
106
107 fp, pathname, description = imp.find_module(module_name, [module_path])
108
109 try:
110 return imp.load_module(module_name, fp, pathname, description)
111 finally:
112 # Since we may exit via an exception, close fp explicitly.
113 if fp:
114 fp.close()
19def _importfrom(modname):
20 # from .edenscmnative import <modname> (where . is looked through this module)
21 fakelocals = {}
22 pkg = __import__("edenscmnative", globals(), fakelocals, [modname], level=0)
23 try:
24 fakelocals[modname] = mod = getattr(pkg, modname)
25 except AttributeError:
26 raise ImportError(r"cannot import name %s" % modname)
27 # force import; fakelocals[modname] may be replaced with the real module
28 getattr(mod, r"__doc__", None)
29 return fakelocals[modname]
365def visit_import(self, node):
366 """return an astroid.Import node as string"""
367 return "import %s" % _import_string(node.names)
69def import_module(module_name):
70 '''
71 Imports the specified module by name.
72 '''
73
74 parts = module_name.split('.')
75 module = __import__(module_name)
76 for part in parts[1:]:
77 module = getattr(module, part)
78 return module
21def import_module(name, package=None):
22 """Import a module.
23
24 The 'package' argument is required when performing a relative import. It
25 specifies the package to use as the anchor point from which to resolve the
26 relative import to an absolute import.
27
28 """
29 if name.startswith('.'):
30 if not package:
31 raise TypeError("relative imports require the 'package' argument")
32 level = 0
33 for character in name:
34 if character != '.':
35 break
36 level += 1
37 name = _resolve_name(name[level:], package, level)
38 m = __import__(name)
39 for subM in name.split('.')[1:]:
40 m = getattr(m, subM)
41 return m
76def Import(self, name: str, *args: Any, **kwargs: Any) -> "Import":
77 return Import(self._module, name, *args, **kwargs)
609def importFrom(modname, objname, asTuple=False):
610 """
611 Import `modname` and return reference to `objname` within the module.
612
613 :param modname: (str) the name of a Python module
614 :param objname: (str) the name of an object in module `modname`
615 :param asTuple: (bool) if True a tuple is returned, otherwise just the object
616 :return: (object or (module, object)) depending on `asTuple`
617 """
618 from importlib import import_module
619
620 module = import_module(modname, package=None)
621 obj = getattr(module, objname)
622 return ((module, obj) if asTuple else obj)

Related snippets