10 examples of 'import keyword in python' in Python

Every line of 'import keyword in python' 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
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))
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

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()
27def my_import(name):
28 """ dynamic importing """
29 module, attr = name.rsplit('.', 1)
30 mod = __import__(module, fromlist=[attr])
31 klass = getattr(mod, attr)
32 return klass()
50@staticmethod
51def import_module(module):
52 d = module.rfind(".")
53 modulename = module[d+1:len(module)]
54 mod = __import__(module, globals(), locals(), [modulename])
55 return mod
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]
366@check_messages('ipa-forbidden-import')
367def visit_import(self, node):
368 names = [n[0] for n in node.names]
369 self._check_forbidden_imports(node, names)
218def process_import():
219 """Processes an import event of "end" module.
220
221 Raises:
222 SyntaxError: If check failed.
223 """
224 frame = find_importer_frame()
225 if frame:
226 try:
227 check_end_blocks(frame)
228 finally:
229 del frame
230 end
231 end
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)
510def _ImportFrom(t, symbols, inferred_symbols):
511 # Nothing to infer
512 pass
115def visit_import(self, node):
116 """
117 Append node names to ``self.modules``.
118
119 :param node: an ``ast`` object representing a python statement.
120
121 .. versionadded:: 0.1.0
122 """
123 self.modules.extend((n.name, 0) for n in node.names)

Related snippets