Python imp.load_module() Examples
The following are 30
code examples of imp.load_module().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
imp
, or try the search function
.
Example #1
Source File: urlparser.py From mishkal with GNU General Public License v3.0 | 6 votes |
def make_py(parser, environ, filename): module = load_module(environ, filename) if not module: return None if hasattr(module, 'application') and module.application: return getattr(module.application, 'wsgi_application', module.application) base_name = module.__name__.split('.')[-1] if hasattr(module, base_name): obj = getattr(module, base_name) if hasattr(obj, 'wsgi_application'): return obj.wsgi_application else: # @@: Old behavior; should probably be deprecated eventually: return getattr(module, base_name)() environ['wsgi.errors'].write( "Cound not find application or %s in %s\n" % (base_name, module)) return None
Example #2
Source File: pydoc.py From jawfish with MIT License | 6 votes |
def importfile(path): """Import a Python source file or compiled file given its path.""" magic = imp.get_magic() with open(path, 'rb') as file: if file.read(len(magic)) == magic: kind = imp.PY_COMPILED else: kind = imp.PY_SOURCE file.seek(0) filename = os.path.basename(path) name, ext = os.path.splitext(filename) try: module = imp.load_module(name, file, path, (ext, 'r', kind)) except: raise ErrorDuringImport(path, sys.exc_info()) return module
Example #3
Source File: pydoc.py From ironpython2 with Apache License 2.0 | 6 votes |
def importfile(path): """Import a Python source file or compiled file given its path.""" magic = imp.get_magic() file = open(path, 'r') if file.read(len(magic)) == magic: kind = imp.PY_COMPILED else: kind = imp.PY_SOURCE file.close() filename = os.path.basename(path) name, ext = os.path.splitext(filename) file = open(path, 'r') try: module = imp.load_module(name, file, path, (ext, 'r', kind)) except: raise ErrorDuringImport(path, sys.exc_info()) file.close() return module
Example #4
Source File: pydoc.py From ironpython2 with Apache License 2.0 | 6 votes |
def synopsis(filename, cache={}): """Get the one-line summary out of a module file.""" mtime = os.stat(filename).st_mtime lastupdate, result = cache.get(filename, (None, None)) if lastupdate is None or lastupdate < mtime: info = inspect.getmoduleinfo(filename) try: file = open(filename) except IOError: # module can't be opened, so skip it return None if info and 'b' in info[2]: # binary modules have to be imported try: module = imp.load_module('__temp__', file, filename, info[1:]) except: return None result = module.__doc__.splitlines()[0] if module.__doc__ else None del sys.modules['__temp__'] else: # text modules can be directly examined result = source_synopsis(file) file.close() cache[filename] = (mtime, result) return result
Example #5
Source File: coverage.py From oscrypto with MIT License | 6 votes |
def _load_package_tests(name): """ Load the test classes from another modularcrypto package :param name: A unicode string of the other package name :return: A list of unittest.TestCase classes of the tests for the package """ package_dir = os.path.join('..', name) if not os.path.exists(package_dir): return [] tests_module_info = imp.find_module('tests', [package_dir]) tests_module = imp.load_module('%s.tests' % name, *tests_module_info) return tests_module.test_classes()
Example #6
Source File: __init__.py From verge3d-blender-addon with GNU General Public License v3.0 | 6 votes |
def _find_and_load_module(self, name, path=None): """ Finds and loads it. But if there's a . in the name, handles it properly. """ bits = name.split('.') while len(bits) > 1: # Treat the first bit as a package packagename = bits.pop(0) package = self._find_and_load_module(packagename, path) try: path = package.__path__ except AttributeError: # This could be e.g. moves. flog.debug('Package {0} has no __path__.'.format(package)) if name in sys.modules: return sys.modules[name] flog.debug('What to do here?') name = bits[0] module_info = imp.find_module(name, path) return imp.load_module(name, *module_info)
Example #7
Source File: __init__.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def find_module(self, fullname, path=None): logger.debug('Running find_module: {0}...'.format(fullname)) if '.' in fullname: parent, child = fullname.rsplit('.', 1) if path is None: loader = self.find_module(parent, path) mod = loader.load_module(parent) path = mod.__path__ fullname = child # Perhaps we should try using the new importlib functionality in Python # 3.3: something like this? # thing = importlib.machinery.PathFinder.find_module(fullname, path) try: self.found = imp.find_module(fullname, path) except Exception as e: logger.debug('Py2Fixer could not find {0}') logger.debug('Exception was: {0})'.format(fullname, e)) return None self.kind = self.found[-1][-1] if self.kind == imp.PKG_DIRECTORY: self.pathname = os.path.join(self.found[1], '__init__.py') elif self.kind == imp.PY_SOURCE: self.pathname = self.found[1] return self
Example #8
Source File: __init__.py From ALF with Apache License 2.0 | 6 votes |
def import_helper(): from os.path import dirname import imp possible_libs = ["_alf_grammar.win32", "_alf_grammar.ntoarm", "_alf_grammar.ntox86", "_alf_grammar.linux"] found_lib = False for i in possible_libs: fp = None try: fp, pathname, description = imp.find_module(i, [dirname(__file__)]) _mod = imp.load_module("_alf_grammar", fp, pathname, description) found_lib = True break except ImportError: pass finally: if fp: fp.close() if not found_lib: raise ImportError("Failed to load _alf_grammar module") return _mod
Example #9
Source File: __init__.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def _find_and_load_module(self, name, path=None): """ Finds and loads it. But if there's a . in the name, handles it properly. """ bits = name.split('.') while len(bits) > 1: # Treat the first bit as a package packagename = bits.pop(0) package = self._find_and_load_module(packagename, path) try: path = package.__path__ except AttributeError: # This could be e.g. moves. flog.debug('Package {0} has no __path__.'.format(package)) if name in sys.modules: return sys.modules[name] flog.debug('What to do here?') name = bits[0] module_info = imp.find_module(name, path) return imp.load_module(name, *module_info)
Example #10
Source File: __init__.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def find_module(self, fullname, path=None): logger.debug('Running find_module: {0}...'.format(fullname)) if '.' in fullname: parent, child = fullname.rsplit('.', 1) if path is None: loader = self.find_module(parent, path) mod = loader.load_module(parent) path = mod.__path__ fullname = child # Perhaps we should try using the new importlib functionality in Python # 3.3: something like this? # thing = importlib.machinery.PathFinder.find_module(fullname, path) try: self.found = imp.find_module(fullname, path) except Exception as e: logger.debug('Py2Fixer could not find {0}') logger.debug('Exception was: {0})'.format(fullname, e)) return None self.kind = self.found[-1][-1] if self.kind == imp.PKG_DIRECTORY: self.pathname = os.path.join(self.found[1], '__init__.py') elif self.kind == imp.PY_SOURCE: self.pathname = self.found[1] return self
Example #11
Source File: py3k.py From lambda-packs with MIT License | 6 votes |
def npy_load_module(name, fn, info=None): """ Load a module. .. versionadded:: 1.11.2 Parameters ---------- name : str Full module name. fn : str Path to module file. info : tuple, optional Only here for backward compatibility with Python 2.*. Returns ------- mod : module """ import importlib.machinery return importlib.machinery.SourceFileLoader(name, fn).load_module()
Example #12
Source File: py3k.py From recruit with Apache License 2.0 | 6 votes |
def npy_load_module(name, fn, info=None): """ Load a module. .. versionadded:: 1.11.2 Parameters ---------- name : str Full module name. fn : str Path to module file. info : tuple, optional Only here for backward compatibility with Python 2.*. Returns ------- mod : module """ import importlib.machinery return importlib.machinery.SourceFileLoader(name, fn).load_module()
Example #13
Source File: sandbox.py From browserscope with Apache License 2.0 | 6 votes |
def _find_and_load_module(self, submodule_name, fullname, path): """Finds and loads a module, using a provided search path. Args: submodule_name: The name of the submodule within its parent package. fullname: The full name of the module to load. path: A list containing the paths to search for the module. Returns: The requested module. Raises: ImportError: The module could not be imported. """ source_file, path_name, description, loader = self._find_module_or_loader( submodule_name, fullname, path) if loader: return loader.load_module(fullname) try: return imp.load_module(fullname, source_file, path_name, description) finally: if source_file: source_file.close()
Example #14
Source File: ihooks.py From meddle with MIT License | 6 votes |
def load_module(self, name, stuff): file, filename, info = stuff (suff, mode, type) = info try: if type == BUILTIN_MODULE: return self.hooks.init_builtin(name) if type == FROZEN_MODULE: return self.hooks.init_frozen(name) if type == C_EXTENSION: m = self.hooks.load_dynamic(name, filename, file) elif type == PY_SOURCE: m = self.hooks.load_source(name, filename, file) elif type == PY_COMPILED: m = self.hooks.load_compiled(name, filename, file) elif type == PKG_DIRECTORY: m = self.hooks.load_package(name, filename, file) else: raise ImportError, "Unrecognized module type (%r) for %s" % \ (type, name) finally: if file: file.close() m.__file__ = filename return m
Example #15
Source File: pydoc.py From meddle with MIT License | 6 votes |
def importfile(path): """Import a Python source file or compiled file given its path.""" magic = imp.get_magic() file = open(path, 'r') if file.read(len(magic)) == magic: kind = imp.PY_COMPILED else: kind = imp.PY_SOURCE file.close() filename = os.path.basename(path) name, ext = os.path.splitext(filename) file = open(path, 'r') try: module = imp.load_module(name, file, path, (ext, 'r', kind)) except: raise ErrorDuringImport(path, sys.exc_info()) file.close() return module
Example #16
Source File: pydoc.py From meddle with MIT License | 6 votes |
def synopsis(filename, cache={}): """Get the one-line summary out of a module file.""" mtime = os.stat(filename).st_mtime lastupdate, result = cache.get(filename, (0, None)) if lastupdate < mtime: info = inspect.getmoduleinfo(filename) try: file = open(filename) except IOError: # module can't be opened, so skip it return None if info and 'b' in info[2]: # binary modules have to be imported try: module = imp.load_module('__temp__', file, filename, info[1:]) except: return None result = (module.__doc__ or '').splitlines()[0] del sys.modules['__temp__'] else: # text modules can be directly examined result = source_synopsis(file) file.close() cache[filename] = (mtime, result) return result
Example #17
Source File: CTPlugin.py From CapTipper with GNU General Public License v3.0 | 6 votes |
def init_plugins(): p_files = glob.glob(CTCore.plugins_folder + "*.py") for p in p_files: p_full = os.path.join(os.path.dirname(os.path.realpath(__file__)),p) (path, name) = os.path.split(p_full) (name, ext) = os.path.splitext(name) (p_file, filename, data) = imp.find_module(name, [path]) mod = imp.load_module(name, p_file, filename, data) for name, value in inspect.getmembers(mod): if inspect.isclass(value): if issubclass(value, ConsolePlugin) and value is not ConsolePlugin: p_num = len(CTCore.plugins) CTCore.plugins.append(namedtuple('Plugin', ['id', 'name','module', 'description'])) CTCore.plugins[p_num].id = p_num CTCore.plugins[p_num].name = name CTCore.plugins[p_num].module = value CTCore.plugins[p_num].description = value.description
Example #18
Source File: plug.py From EDMarketConnector with GNU General Public License v2.0 | 6 votes |
def __init__(self, name, loadfile): """ Load a single plugin :param name: module name :param loadfile: the main .py file :raises Exception: Typically ImportError or OSError """ self.name = name # Display name. self.folder = name # basename of plugin folder. None for internal plugins. self.module = None # None for disabled plugins. if loadfile: sys.stdout.write(('loading plugin %s from "%s"\n' % (name.replace('.', '_'), loadfile)).encode('utf-8')) with open(loadfile, 'rb') as plugfile: module = imp.load_module('plugin_%s' % name.encode('ascii', 'replace').replace('.', '_'), plugfile, loadfile.encode(sys.getfilesystemencoding()), ('.py', 'r', imp.PY_SOURCE)) if module.plugin_start.func_code.co_argcount == 0: newname = module.plugin_start() else: newname = module.plugin_start(os.path.dirname(loadfile)) self.name = newname and unicode(newname) or name self.module = module else: sys.stdout.write('plugin %s disabled\n' % name)
Example #19
Source File: extloader.py From westpa with MIT License | 5 votes |
def get_object(object_name, path=None): """Attempt to load the given object, using additional path information if given.""" try: (modspec, symbol) = object_name.rsplit('.', 1) except ValueError: # no period found raise ValueError("object_name name must be in the form 'module.symbol'") log.debug('attempting to load %r from %r' % (symbol, modspec)) module = load_module(modspec, path) # This will raise AttributeError (as expected) if the symbol is not in the module return getattr(module, symbol)
Example #20
Source File: depends.py From python-netsurv with MIT License | 5 votes |
def get_module_constant(module, symbol, default=-1, paths=None): """Find 'module' by searching 'paths', and extract 'symbol' Return 'None' if 'module' does not exist on 'paths', or it does not define 'symbol'. If the module defines 'symbol' as a constant, return the constant. Otherwise, return 'default'.""" try: f, path, (suffix, mode, kind) = find_module(module, paths) except ImportError: # Module doesn't exist return None try: if kind == PY_COMPILED: f.read(8) # skip magic & date code = marshal.load(f) elif kind == PY_FROZEN: code = imp.get_frozen_object(module) elif kind == PY_SOURCE: code = compile(f.read(), path, 'exec') else: # Not something we can parse; we'll have to import it. :( if module not in sys.modules: imp.load_module(module, f, path, (suffix, mode, kind)) return getattr(sys.modules[module], symbol, None) finally: if f: f.close() return extract_constant(code, symbol, default)
Example #21
Source File: cli.py From yasha with MIT License | 5 votes |
def load_python_module(file): try: from importlib.machinery import SourceFileLoader loader = SourceFileLoader('yasha_extensions', file.name) module = loader.load_module() except ImportError: # Fallback to Python2 import imp desc = (".py", "rb", imp.PY_SOURCE) module = imp.load_module('yasha_extensions', file, file.name, desc) return module
Example #22
Source File: py3k.py From lambda-packs with MIT License | 5 votes |
def npy_load_module(name, fn, info=None): """ Load a module. .. versionadded:: 1.11.2 Parameters ---------- name : str Full module name. fn : str Path to module file. info : tuple, optional Information as returned by `imp.find_module` (suffix, mode, type). Returns ------- mod : module """ import imp import os if info is None: path = os.path.dirname(fn) fo, fn, info = imp.find_module(name, [path]) else: fo = open(fn, info[1]) try: mod = imp.load_module(name, fo, fn, info) finally: fo.close() return mod
Example #23
Source File: test_importhooks.py From ironpython2 with Apache License 2.0 | 5 votes |
def load_module(self, fullname): raise ImportError, "I dare you"
Example #24
Source File: plugin_loader.py From cascade-server with Apache License 2.0 | 5 votes |
def load_plugins(self): for plugin_name, plugin_found_module in self.plugins.items(): logger.info("[PLUGINS] Loading {}".format(plugin_name)) try: imp.load_module(plugin_name, *plugin_found_module) except ImportError as e: logger.warning("[PLUGINS] Loading {} failed with {}".format(plugin_name, e)) finally: plugin_found_module[0].close() # Close the module file.
Example #25
Source File: depends.py From python-netsurv with MIT License | 5 votes |
def get_module_constant(module, symbol, default=-1, paths=None): """Find 'module' by searching 'paths', and extract 'symbol' Return 'None' if 'module' does not exist on 'paths', or it does not define 'symbol'. If the module defines 'symbol' as a constant, return the constant. Otherwise, return 'default'.""" try: f, path, (suffix, mode, kind) = find_module(module, paths) except ImportError: # Module doesn't exist return None try: if kind == PY_COMPILED: f.read(8) # skip magic & date code = marshal.load(f) elif kind == PY_FROZEN: code = imp.get_frozen_object(module) elif kind == PY_SOURCE: code = compile(f.read(), path, 'exec') else: # Not something we can parse; we'll have to import it. :( if module not in sys.modules: imp.load_module(module, f, path, (suffix, mode, kind)) return getattr(sys.modules[module], symbol, None) finally: if f: f.close() return extract_constant(code, symbol, default)
Example #26
Source File: pkgutil.py From ironpython2 with Apache License 2.0 | 5 votes |
def get_data(package, resource): """Get a resource from a package. This is a wrapper round the PEP 302 loader get_data API. The package argument should be the name of a package, in standard module format (foo.bar). The resource argument should be in the form of a relative filename, using '/' as the path separator. The parent directory name '..' is not allowed, and nor is a rooted name (starting with a '/'). The function returns a binary string, which is the contents of the specified resource. For packages located in the filesystem, which have already been imported, this is the rough equivalent of d = os.path.dirname(sys.modules[package].__file__) data = open(os.path.join(d, resource), 'rb').read() If the package cannot be located or loaded, or it uses a PEP 302 loader which does not support get_data(), then None is returned. """ loader = get_loader(package) if loader is None or not hasattr(loader, 'get_data'): return None mod = sys.modules.get(package) or loader.load_module(package) if mod is None or not hasattr(mod, '__file__'): return None # Modify the resource name to be compatible with the loader.get_data # signature - an os.path format "filename" starting with the dirname of # the package's __file__ parts = resource.split('/') parts.insert(0, os.path.dirname(mod.__file__)) resource_name = os.path.join(*parts) return loader.get_data(resource_name)
Example #27
Source File: pkgutil.py From ironpython2 with Apache License 2.0 | 5 votes |
def load_module(self, fullname): self._reopen() try: mod = imp.load_module(fullname, self.file, self.filename, self.etc) finally: if self.file: self.file.close() # Note: we don't set __loader__ because we want the module to look # normal; i.e. this is just a wrapper for standard import machinery return mod
Example #28
Source File: pydoc.py From meddle with MIT License | 5 votes |
def run(self, callback, key=None, completer=None, onerror=None): if key: key = lower(key) self.quit = False seen = {} for modname in sys.builtin_module_names: if modname != '__main__': seen[modname] = 1 if key is None: callback(None, modname, '') else: desc = split(__import__(modname).__doc__ or '', '\n')[0] if find(lower(modname + ' - ' + desc), key) >= 0: callback(None, modname, desc) for importer, modname, ispkg in pkgutil.walk_packages(onerror=onerror): if self.quit: break if key is None: callback(None, modname, '') else: loader = importer.find_module(modname) if hasattr(loader,'get_source'): import StringIO desc = source_synopsis( StringIO.StringIO(loader.get_source(modname)) ) or '' if hasattr(loader,'get_filename'): path = loader.get_filename(modname) else: path = None else: module = loader.load_module(modname) desc = (module.__doc__ or '').splitlines()[0] path = getattr(module,'__file__',None) if find(lower(modname + ' - ' + desc), key) >= 0: callback(path, modname, desc) if completer: completer()
Example #29
Source File: test_importhooks.py From ironpython2 with Apache License 2.0 | 5 votes |
def load_module(self, fullname): mod = imp.load_module(fullname, self.file, self.filename, self.stuff) if self.file: self.file.close() mod.__loader__ = self # for introspection return mod
Example #30
Source File: pkgutil.py From meddle with MIT License | 5 votes |
def load_module(self, fullname): self._reopen() try: mod = imp.load_module(fullname, self.file, self.filename, self.etc) finally: if self.file: self.file.close() # Note: we don't set __loader__ because we want the module to look # normal; i.e. this is just a wrapper for standard import machinery return mod