10 examples of 'import string in python' in Python

Every line of 'import string 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
43def import_string(import_name, silent=False):
44 """Imports an object based on a string. This is useful if you want to
45 use import paths as endpoints or something similar. An import path can
46 be specified either in dotted notation (``xml.sax.saxutils.escape``)
47 or with a colon as object delimiter (``xml.sax.saxutils:escape``).
48
49 If `silent` is True the return value will be `None` if the import fails.
50
51 :param import_name: the dotted name for the object to import.
52 :param silent: if set to `True` import errors are ignored and
53 `None` is returned instead.
54 :return: imported object
55 """
56 try:
57 if ':' in import_name:
58 module, obj = import_name.split(':', 1)
59 elif '.' in import_name:
60 module, obj = import_name.rsplit('.', 1)
61 else:
62 return __import__(import_name)
63 return getattr(__import__(module, None, None, [obj]), obj)
64 except (ImportError, AttributeError):
65 if not silent:
66 raise
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()
141def import_string(module_name, package=None):
142 """
143 import a module or class by string path.
144
145 :module_name: str with path of module or path to import and
146 instanciate a class
147 :returns: a module object or one instance from class if
148 module_name is a valid path to class
149
150 """
151 module, klass = module_name.rsplit(".", 1)
152 module = import_module(module, package=package)
153 obj = getattr(module, klass)
154 if ismodule(obj):
155 return obj
156 return obj()
11def import_module(name):
12 __import__("%s" % (name,), globals(), locals(), [], -1)
13 return sys.modules[name]
28def import_module(name):
29 """
30 Imports a Python module assuming that the whole *name* identifies only a
31 Python module and no symbol inside a Python module.
32 """
33
34 # fromlist must not be empty so we get the bottom-level module rather than
35 # the top-level module.
36 return __import__(name, fromlist=[''])
56def _import_module(importer, module_name, package):
57 """Import a module dynamically into a package.
58
59 :param importer: PEP302 Importer object (which knows the path to look in).
60 :param module_name: the name of the module to import.
61 :param package: the package to import the module into.
62 """
63
64 # Duplicate copies of modules are bad, so check if this has already been
65 # imported statically
66 if module_name in sys.modules:
67 return sys.modules[module_name]
68
69 loader = importer.find_module(module_name)
70 if loader is None:
71 return None
72
73 module = loader.load_module(module_name)
74
75 # Make this accessible through the parent package for static imports
76 local_name = module_name.partition(package.__name__ + '.')[2]
77 module_components = local_name.split('.')
78 parent = functools.reduce(getattr, module_components[:-1], package)
79 setattr(parent, module_components[-1], module)
80
81 return module
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
680def _import_module(module):
681 __import__(module)
682 return sys.modules[module]
35def import_module(module_name):
36 module = __import__(module_name)
37 if '.' in module_name:
38 return reduce(getattr, module_name.split('.')[1:], module)
39 return module
9def import_module(module_string):
10 return import_string(module_string)

Related snippets