8 examples of 'python reimport' in Python

Every line of 'python reimport' 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
27def __import__(self, name, globals=None, locals=None, fromlist=(), level=0): # noqa
28 module = self._orig___import__(name, globals, locals, fromlist, level)
29
30 self.reload(module)
31
32 if fromlist:
33 from_names = [
34 name
35 for item in fromlist
36 for name in (
37 getattr(module, '__all__', []) if item == '*' else (item,)
38 )
39 ]
40
41 for name in from_names:
42 value = getattr(module, name, None)
43 if ismodule(value):
44 self.reload(value)
45
46 return module
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

59def importMod():
60 with warnings.catch_warnings(record=True) as w:
61 mod = importlib.__import__(f)
62 sys.modules[fullname] = mod
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()
8def try_import_ipython():
9 try:
10 return import_module('IPython')
11 except ImportError:
12 return None
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]
366def reload(self, module):
367 name = module.__name__
368 if '.' in name:
369 i = name.rfind('.')
370 head, tail = name[:i], name[i+1:]
371 path = sys.modules[head].__path__
372 else:
373 tail = name
374 path = sys.modules[''].__path__
375 stuff = self.loader.find_module(tail, path)
376 if not stuff:
377 raise ImportError, "No module named %s" % name
378 return self.loader.load_module(name, stuff)
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()
4def import_dir(name, fromlist=()):
5 PACKAGE_EXT = '.sublime-package'
6 dirname = os.path.basename(os.path.dirname(os.path.realpath(__file__)))
7 if dirname.endswith(PACKAGE_EXT):
8 dirname = dirname[:-len(PACKAGE_EXT)]
9 return __import__('{0}.{1}'.format(dirname, name), fromlist=fromlist)

Related snippets