def module_exists(module_name, comment): try: imp.find_module(module_name) except ImportError: print "You have to install "+module_name print comment exit()
def import_pyv8(): # Importing non-existing modules is a bit tricky in Python: # if we simply call `import PyV8` and module doesn't exists, # Python will cache this failed import and will always # throw exception even if this module appear in PYTHONPATH. # To prevent this, we have to manually test if # PyV8.py(c) exists in PYTHONPATH before importing PyV8 if 'PyV8' in sys.modules and 'PyV8' not in globals(): # PyV8 was loaded by ST2, create global alias globals()['PyV8'] = __import__('PyV8') return loaded = False f, pathname, description = imp.find_module('PyV8') bin_f, bin_pathname, bin_description = imp.find_module('_PyV8') if f: try: imp.acquire_lock() globals()['_PyV8'] = imp.load_module('_PyV8', bin_f, bin_pathname, bin_description) globals()['PyV8'] = imp.load_module('PyV8', f, pathname, description) imp.release_lock() loaded = True finally: # Since we may exit via an exception, close fp explicitly. if f: f.close() if bin_f: bin_f.close() if not loaded: raise ImportError('No PyV8 module found')
def readfile(name): tokens = name.split(".") f = None token = tokens[0] token_name = [token] token_str = ".".join(token_name) try: f, pathname, description = imp.find_module(token_str) for token in tokens[1:]: module = imp.load_module(token_str, f, pathname, description) if f is not None: f.close() token_name.append(token) token_str = ".".join(token_name) f, pathname, description = imp.find_module( token, module.__path__) if f is not None: contents = f.read() else: contents = "" finally: if f is not None: f.close() return contents.encode("UTF8")
def find_management_module(app_name): """ Determines the path to the management module for the given app_name, without actually importing the application or the management module. Raises ImportError if the management module cannot be found for any reason. """ parts = app_name.split(".") parts.append("management") parts.reverse() part = parts.pop() path = None # When using manage.py, the project module is added to the path, # loaded, then removed from the path. This means that # testproject.testapp.models can be loaded in future, even if # testproject isn't in the path. When looking for the management # module, we need look for the case where the project name is part # of the app_name but the project directory itself isn't on the path. try: f, path, descr = imp.find_module(part, path) except ImportError as e: if os.path.basename(os.getcwd()) != part: raise e while parts: part = parts.pop() f, path, descr = imp.find_module(part, path and [path] or None) return path
def import_module(module_name): """ Imports a module. A single point of truth for importing modules to be documented by `pdoc`. In particular, it makes sure that the top module in `module_name` can be imported by using only the paths in `pdoc.import_path`. If a module has already been imported, then its corresponding entry in `sys.modules` is returned. This means that modules that have changed on disk cannot be re-imported in the same process and have its documentation updated. """ if import_path != sys.path: # Such a kludge. Only restrict imports if the `import_path` has # been changed. We don't want to always restrict imports, since # providing a path to `imp.find_module` stops it from searching # in special locations for built ins or frozen modules. # # The problem here is that this relies on the `sys.path` not being # independently changed since the initialization of this module. # If it is changed, then some packages may fail. # # Any other options available? # Raises an exception if the parent module cannot be imported. # This hopefully ensures that we only explicitly import modules # contained in `pdoc.import_path`. imp.find_module(module_name.split('.')[0], import_path) if module_name in sys.modules: return sys.modules[module_name] else: __import__(module_name) return sys.modules[module_name]
def load_module(self, module_path): """ uses imp to load a module """ loaded_module = None module_name = os.path.basename(module_path) package_path = os.path.dirname(module_path) pkg_name = os.path.basename(package_path) root_path = os.path.dirname(package_path) if module_name.find(".py") != -1: module_name = module_name.split(".")[0] f, p_path, description = find_module(pkg_name, [root_path]) loaded_pkg = load_module(pkg_name, f, p_path, description) f, m_path, description = find_module( module_name, loaded_pkg.__path__) try: mod = "{0}.{1}".format(loaded_pkg.__name__, module_name) loaded_module = load_module(mod, f, m_path, description) except ImportError: raise return loaded_module
def find_commands_module(app_name): """ Find the commands module in each app (if it exists) and return the path app_name : The name of an app in the INSTALLED_APPS setting return - path to the app """ parts = app_name.split('.') parts.append('commands') parts.reverse() part = parts.pop() path = None #Load the module if needed try: f, path, descr = imp.find_module(part, path) except ImportError as e: if os.path.basename(os.getcwd()) != part: raise e else: try: if f: f.close() except UnboundLocalError: log.error("Could not import module {0} at path {1}. Sys.path is {2}".format(part, path, sys.path)) #Go down level by and level and try to load the module at each level while parts: part = parts.pop() f, path, descr = imp.find_module(part, [path] if path else None) if f: f.close() return path
def clean_tool(self): """ Checks the SCMTool used for this repository for dependencies. If one or more dependencies aren't found, they will be presented as validation errors. """ tool = self.cleaned_data['tool'] scmtool_class = tool.get_scmtool_class() errors = [] for dep in scmtool_class.dependencies.get('modules', []): try: imp.find_module(dep) except ImportError: errors.append('The Python module "%s" is not installed.' 'You may need to restart the server ' 'after installing it.' % dep) for dep in scmtool_class.dependencies.get('executables', []): if not is_exe_in_path(dep): if sys.platform == 'win32': exe_name = '%s.exe' % dep else: exe_name = dep errors.append('The executable "%s" is not in the path.' % exe_name) if errors: raise forms.ValidationError(errors) return tool
def is_module_for_sure(mod, search_path=sys.path): mod = mod.replace('(', '') # Ignore parentheses try: mod_name = mod while '.' in mod_name: pack_name, _sep, mod_name = mod.partition('.') f, p, d = imp.find_module(pack_name, search_path) search_path = [p] imp.find_module(mod_name, search_path) except ImportError: try: # NOTE(vish): handle namespace modules __import__(mod) except ImportError as exc: # NOTE(vish): the import error might be due # to a missing dependency missing = str(exc).split()[-1] if (missing != mod.split('.')[-1] or "cannot import" in str(exc)): return True return False except Exception: # NOTE(jogo) don't stack trace if unexpected import error, # log and continue. traceback.print_exc() return False return True
def mod_exists(mod): """Check whether module is known to exist.""" try: imp.find_module(mod) return True except ImportError: return False
def pluginImport(self, name, path=None): """Import a single plugin""" if path is not None: try: self.info('loading plugin from specified path : %s', path) fp, pathname, description = imp.find_module(name, [path]) try: return imp.load_module(name, fp, pathname, description) finally: if fp: fp.close() except ImportError: pass try: module = 'b3.plugins.%s' % name mod = __import__(module) components = module.split('.') for comp in components[1:]: mod = getattr(mod, comp) return mod except ImportError, m: self.info('Could not load built in plugin %s (%s)', name, m) self.info('trying external plugin directory : %s', self.config.getpath('plugins', 'external_dir')) fp, pathname, description = imp.find_module(name, [self.config.getpath('plugins', 'external_dir')]) try: return imp.load_module(name, fp, pathname, description) finally: if fp: fp.close()
def autodiscover(): """ Automatically register all bbcode tags. This searches the 'bbtags' modules of all INSTALLED_APPS if available. """ global AUTODISCOVERED if AUTODISCOVERED: return import imp from django.conf import settings import os for app in settings.INSTALLED_APPS: try: module = __import__(app, {}, {}, [app.split('.')[-1]]) app_path = module.__path__ except AttributeError: continue try: imp.find_module('bbtags', app_path) except ImportError: continue for f in os.listdir(os.path.join(os.path.dirname(os.path.abspath(module.__file__)), 'bbtags')): mod_name, ext = os.path.splitext(f) if ext == '.py': __import__("%s.bbtags.%s" % (app, mod_name)) AUTODISCOVERED = True
def testModuleExists(mod): try: imp.find_module(mod) except ImportError: print "!!! Module " + mod + " doesn't exist" print "No fancy particle names for you!" args.rawNames = True
def _tool_module(self): oldpythonpath = sys.path sys.path = self.toolpath + sys.path try: try: file, path, desc = imp.find_module(self.name, self.toolpath) try: return imp.load_module(self.name, file, path, desc) finally: if file: file.close() except ImportError, e: pass finally: sys.path = oldpythonpath full_name = "SCons.Tool." + self.name try: return sys.modules[full_name] except KeyError: try: smpath = sys.modules["SCons.Tool"].__path__ file, path, desc = imp.find_module(self.name, smpath) try: module = imp.load_module(full_name, file, path, desc) setattr(SCons.Tool, self.name, module) return module finally: if file: file.close() except ImportError, e: m = "No tool named '%s': %s" % (self.name, e) raise SCons.Errors.UserError, m
def _find_module(module_name): name = _get_name(module_name) package = get_package(module_name) if package: try: found = find_module(name, package.__path__) except ImportError: # It appears that we are expeted to load either: # # sample.sample.factorial # or # sample.factorial # # Both expect us to load the same module. For now basically catch # InportError in find_module, then try again stripping off the # first module. I'm not sure yet how this is supposed to be done. package = get_package(module_name, depth=2) if package: found = find_module(name, package.__path__) else: # With the six test this causes things to break #found = find_module(name) found = None else: found = find_module(module_name) return found
def autodiscover(): import imp from django.conf import settings for app in settings.CONFIGURED_APPS: # For each app, we need to look for an search_indexes.py inside that app's # package. We can't use os.path here -- recall that modules may be # imported different ways (think zip files) -- so we need to get # the app's __path__ and look for search_indexes.py on that path. # Step 1: find out the app's __path__ Import errors here will (and # should) bubble up, but a missing __path__ (which is legal, but weird) # fails silently -- apps that do weird things with __path__ might # need to roll their own index registration. try: app_path = __import__(app, {}, {}, [app.split('.')[-1]]).__path__ except AttributeError: continue # Step 2: use imp.find_module to find the app's search_indexes.py. For some # reason imp.find_module raises ImportError if the app can't be found # but doesn't actually try to import the module. So skip this app if # its search_indexes.py doesn't exist try: imp.find_module('config_definitions', app_path) except ImportError: continue # Step 3: import the app's search_index file. If this has errors we want them # to bubble up. __import__("%s.config_definitions" % app)
def Use(name): # print(dir(sys.modules['solidpy'])); exit() if name.endswith(".solid.py"): with open(name) as f: code = compile(f.read(), name, "exec") exec(code, script_globals) # , script_locals) elif name.endswith(".scad"): Defaults.includeFiles.append(name) else: import imp try: fname = imp.find_module(name)[1] except: try: fname = imp.find_module(name + ".solid")[1] except: fname = imp.find_module(name + "/" + name + ".solid")[1] if fname is None: code = "from " + name + " import *" exec(code, script_globals) # , script_locals) else: with open(fname) as f: code = compile(f.read(), fname, "exec") exec(code, script_globals) # , script_locals)
def build(buildargs=['-y', '-windowed', '--onedir', '--clean', '--icon="icons/desuratools.ico"', '--noupx', '--version-file=versioninfo.txt'], package=True): pyinstaller = os.path.join(site.getsitepackages()[0], "Scripts", "pyinstaller-script.py") dependencies = ['PySide', 'PIL', 'win32api', 'win32gui', 'win32ui', 'win32con', 'requests'] imageformats = os.path.join(site.getsitepackages()[1], "PySide", "plugins", "imageformats") buildargs.insert(0, 'desuratools.py') buildargs.insert(0, pyinstaller) buildargs.insert(0, "python") dist_folder = os.path.join(os.getcwd(), "dist") output_folder = os.path.join(os.getcwd(), "dist", "desuratools") if not os.path.exists(pyinstaller): raise IOError("PyInstaller is required to build for windows") print "PyInstaller check passed" for module in dependencies: try: imp.find_module(module) except ImportError: raise ImportError("Dependency {0} is required".format(module)) print "Dependency check passed" print "Building DesuraTools" subprocess.call(' '.join(buildargs)) print "Copying imageformat plugins" imageformats_dist = os.path.join(output_folder, "imageformats") distutils.dir_util.copy_tree(imageformats, imageformats_dist, verbose=1) print "Copying icon" images_dist = os.path.join(output_folder, "desuratools_256.png") shutil.copyfile("desuratools_256.png", images_dist) if package: package_app(dist_folder, output_folder)
def autodiscover(): """ Goes and imports the permissions submodule of every app in INSTALLED_APPS to make sure the templates classes/fns are registered correctly. """ global LOADING if LOADING: return LOADING = True import imp from django.conf import settings for app in settings.INSTALLED_APPS: try: __import__(app) app_path = sys.modules[app].__path__ except AttributeError: continue try: imp.find_module('enlivetmpl', app_path) except ImportError: continue __import__("%s.enlivetmpl" % app) app_path = sys.modules["%s.enlivetmpl" % app] LOADING = False
def __init__(self, method, test=None, arg=tuple(), descriptor=None): """Initialize the MethodTestCase. """ self.method = method self.test = test self.arg = arg self.descriptor = descriptor self.cls = method.im_class self.inst = self.cls() if self.test is None: method_name = self.method.__name__ self.test = getattr(self.inst, method_name) TestBase.__init__(self) self.suite = TestBase.Suite() self.suite.__module__, self.suite.__name__ = self.__get_module() has_module = True try: imp.find_module(self.suite.__module__)[1] except ImportError: has_module = False if sys.version.find("IronPython") != -1 or not has_module: # Iron Python doesn't fully support imp self.suite.abs_location = "" else: self.suite.abs_location = "file://" + imp.find_module(self.suite.__module__)[1] self.suite.location = "python_uttestid://" + self.suite.__module__ + "." + self.suite.__name__
def is_module_for_sure(mod, search_path=sys.path): mod = mod.replace('(', '') # Ignore parentheses try: mod_name = mod while '.' in mod_name: pack_name, _sep, mod_name = mod.partition('.') f, p, d = imp.find_module(pack_name, search_path) search_path = [p] imp.find_module(mod_name, search_path) except ImportError: try: # NOTE(vish): handle namespace modules if '.' in mod: pack_name, mod_name = mod.rsplit('.', 1) __import__(pack_name, fromlist=[mod_name]) else: __import__(mod) except ImportError: # NOTE(imelnikov): import error here means the thing is # not importable in current environment, either because # of missing dependency, typo in code being checked, or # any other reason. Anyway, we have no means to know if # it is module or not, so we return True to avoid # false positives. return True except Exception: # NOTE(jogo) don't stack trace if unexpected import error, # log and continue. traceback.print_exc() return False else: # NOTE(imelnikov): we imported the thing; if it was module, # it must be there: return mod in sys.modules return True
def dajaxice_autodiscover(): """ Auto-discover INSTALLED_APPS ajax.py modules and fail silently when not present. NOTE: dajaxice_autodiscover was inspired/copied from django.contrib.admin autodiscover """ global LOADING_DAJAXICE if LOADING_DAJAXICE: return LOADING_DAJAXICE = True import imp from django.conf import settings for app in settings.INSTALLED_APPS: try: app_path = import_module(app).__path__ except AttributeError: continue try: imp.find_module('ajax', app_path) except ImportError: continue import_module("%s.ajax" % app) LOADING_DAJAXICE = False
def validate(cls, value): if not value: return value if value == '<PATHS>': caffe = cls.find_executable('caffe') if not caffe: raise ValueError('caffe binary not found in PATH') cls.validate_version(caffe) try: imp.find_module('caffe') except ImportError: raise ValueError('caffe python package not found in PYTHONPATH') return value else: value = os.path.abspath(value) if not os.path.isdir(value): raise ValueError('"%s" is not a directory' % value) #expected_path = os.path.join(value, 'bin', 'caffe.bin') expected_path = os.path.join(value, 'build', 'tools', 'caffe.bin') if not os.path.exists(expected_path): raise ValueError('caffe binary not found at "%s"' % value) cls.validate_version(expected_path) pythonpath = os.path.join(value, 'python') sys.path.insert(0, pythonpath) try: imp.find_module('caffe') except ImportError as e: raise ValueError('Error while importing caffe from "%s": %s' % (pythonpath, e.message)) finally: # Don't actually add this until apply() is called sys.path.pop(0) return value
def np_comparison_check(h2o_data, np_data, num_elements): """ Check values achieved by h2o against values achieved by numpy :param h2o_data: an H2OFrame or H2OVec :param np_data: a numpy array :param num_elements: number of elements to compare :return: None """ # Check for numpy try: imp.find_module('numpy') except ImportError: assert False, "failed comparison check because unable to import numpy" import numpy as np rows, cols = h2o_data.dim for i in range(num_elements): r = random.randint(0,rows-1) c = random.randint(0,cols-1) h2o_val = h2o_data[r,c] if isinstance(h2o_data,H2OFrame) else h2o_data[r] np_val = np_data[r,c] if len(np_data.shape) > 1 else np_data[r] if isinstance(np_val, np.bool_): np_val = bool(np_val) # numpy haz special bool type :( assert np.absolute(h2o_val - np_val) < 1e-6, \ "failed comparison check! h2o computed {0} and numpy computed {1}".format(h2o_val, np_val)
def _read_credentials(): bpy.rffi_creds_found = False bpy.rffi_user = "" bpy.rffi_hash = "" pwfile = bpy.utils.user_resource("CONFIG", "rffi", True) try: pwmod = imp.find_module("rffi_credentials", [pwfile]) except ImportError: _write_credentials("", "") pwmod = imp.find_module("rffi_credentials", [pwfile]) try: user_creds = imp.load_module("rffi_credentials", pwmod[0], pwmod[1], pwmod[2]) bpy.rffi_user = user_creds.user bpy.rffi_hash = user_creds.hash bpy.rffi_creds_found = True except ImportError: # doesn't exist yet, write template _write_credentials("", "") pwfile = bpy.utils.user_resource("CONFIG", "rffi", True) pwmod = imp.find_module("rffi_credentials", [pwfile]) try: user_creds = imp.load_module("rffi_credentials", pwmod[0], pwmod[1], pwmod[2]) bpy.rffi_user = user_creds.user bpy.rffi_hash = user_creds.hash bpy.rffi_creds_found = True except Exception as e2: print("Couldn't write rffi_credentials.py", e2) finally: if pwmod and pwmod[0]: pwmod[0].close() return bpy.rffi_creds_found
def follow_str(ns, string): debug.dbg('follow_module', ns, string) path = None if ns: path = ns[1] elif self.import_stmt.relative_count: module = self.import_stmt.get_parent_until() path = os.path.abspath(module.path) for i in range(self.import_stmt.relative_count): path = os.path.dirname(path) global imports_processed imports_processed += 1 if path is not None: return imp.find_module(string, [path]) else: debug.dbg('search_module', string, self.file_path) # Override the sys.path. It works only good that way. # Injecting the path directly into `find_module` did not work. sys.path, temp = sys_path_mod, sys.path try: i = imp.find_module(string) except ImportError: sys.path = temp raise sys.path = temp return i
def setUp(self): B3TestCase.setUp(self) when(self.console.config).get_external_plugins_dir().thenReturn(external_plugins_dir) self.conf = CfgConfigParser(testplugin_config_file) self.plugin_list = [ {'name': 'admin', 'conf': '@b3/conf/plugin_admin.ini', 'path': None, 'disabled': False}, ] fp, pathname, description = imp.find_module('testplugin1', [os.path.join(b3.getB3Path(True), '..', 'tests', 'plugins', 'fakeplugins')]) pluginModule1 = imp.load_module('testplugin1', fp, pathname, description) if fp: fp.close() fp, pathname, description = imp.find_module('testplugin3', [os.path.join(b3.getB3Path(True), '..', 'tests', 'plugins', 'fakeplugins')]) pluginModule3 = imp.load_module('testplugin3', fp, pathname, description) if fp: fp.close() fp, pathname, description = imp.find_module('admin', [os.path.join(b3.getB3Path(True), 'plugins')]) adminModule = imp.load_module('admin', fp, pathname, description) if fp: fp.close() when(self.console.config).get_plugins().thenReturn(self.plugin_list) when(self.console).pluginImport('admin', ANY).thenReturn(adminModule) when(self.console).pluginImport('testplugin1', ANY).thenReturn(pluginModule1) when(self.console).pluginImport('testplugin3', ANY).thenReturn(pluginModule3)
def import_project(): ''' Import moderator from project root and register all models it contains with moderation. The project root file allows you to add moderation for models that are in libraries outside the project. ''' from django.conf import settings from django.utils.importlib import import_module import imp import sys project_root = settings.ROOT_URLCONF.split(".")[0] try: app_path = import_module(project_root).__path__ except AttributeError: return None try: imp.find_module('moderator', app_path) except ImportError: return None module = import_module("%s.moderator" % project_root) return module
def __init__(self, **kwargs): """Keyword arguments to the constructor become attributes of this object, which is passed to all functions in the device-specific module.""" for k, v in kwargs.iteritems(): setattr(self, k, v) self.extras = OPTIONS.extras if self.module is None: path = OPTIONS.device_specific if not path: return try: if os.path.isdir(path): info = imp.find_module("releasetools", [path]) else: d, f = os.path.split(path) b, x = os.path.splitext(f) if x == ".py": f = b info = imp.find_module(f, [d]) print "loaded device-specific extensions from", path self.module = imp.load_module("device_specific", *info) except ImportError: print "unable to load device-specific module; assuming none"
def import_module(self, path): if os.path.isfile(path): sys.path.insert(0, os.path.dirname(path)) name = os.path.split(path)[-1].split('.')[0] filename, pathname, description = imp.find_module(name, [os.path.dirname(path)]) module = imp.load_module(name, filename, pathname, description) module.functest_module_path = path module.__file__ = os.path.abspath(path) sys.path.pop(0) elif os.path.isdir(path): if os.path.isfile(os.path.join(path, '__init__.py')): sys.path.insert(0, os.path.abspath(os.path.join(path, os.path.pardir))) name = os.path.split(path)[-1] filename, pathname, description = imp.find_module( name, [os.path.abspath(os.path.join(path, os.path.pardir))]) module = imp.load_module(name, filename, pathname, description) module.functest_module_path = path module.__file__ = os.path.abspath(os.path.join(path, '__init__.py')) sys.path.pop(0) else: module = new.module(os.path.split(path)[-1]) module.functest_module_path = path else: raise ImportError('path is not file or directory') return module
'openassessment.xblock', # edxval 'edxval', # Organizations App (http://github.com/edx/edx-organizations) 'organizations', ) for app_name in OPTIONAL_APPS: # First attempt to only find the module rather than actually importing it, # to avoid circular references - only try to import if it can't be found # by find_module, which doesn't work with import hooks try: imp.find_module(app_name) except ImportError: try: __import__(app_name) except ImportError: continue INSTALLED_APPS += (app_name,) ### ADVANCED_SECURITY_CONFIG # Empty by default ADVANCED_SECURITY_CONFIG = {} ### External auth usage -- prefixes for ENROLLMENT_DOMAIN SHIBBOLETH_DOMAIN_PREFIX = 'shib:' OPENID_DOMAIN_PREFIX = 'openid:'
prev_path = os.path.dirname(opsbro_test_import.__file__) del opsbro_test_import # But to be sure future opsbro import will load the new one, we need to # first hard unload the opsbro modules from python # NOTE: this is only ok because we are in the setup.py, don't do this outside this scope! all_modules = list(sys.modules.keys()) for modname in all_modules: if modname == 'opsbro' or modname.startswith('opsbro.'): del sys.modules[modname] except ImportError as exp: # great, first install so pass # Now look at loading the local opsbro lib for version and banner my_dir = os.path.dirname(os.path.abspath(__file__)) opsbro = imp.load_module( 'opsbro', *imp.find_module('opsbro', [os.path.realpath(my_dir)])) from opsbro.info import VERSION, BANNER, TXT_BANNER from opsbro.log import cprint, is_tty, sprintf, core_logger from opsbro.misc.bro_quotes import get_quote from opsbro.systempacketmanager import get_systepacketmgr from opsbro.cli_display import print_h1 from opsbro.characters import CHARACTERS systepacketmgr = get_systepacketmgr() ################################## Only root as it's a global system tool. if os.name != 'nt' and os.getuid() != 0: cprint('Setup must be launched as root.', color='red') sys.exit(2) # By default logger should not print anything
import imp from password import passwd table_functions = imp.load_module('reformat', *imp.find_module('reformat', ['data'])) DATABASE_CONFIG = {'user': '******', 'db': 'bip3', 'pw': passwd} ERSATZPG_CONFIG = {'debug': True} ERSATZPG_CONFIG.update(DATABASE_CONFIG)
def test_imp_find_module_builtins(self): self.assertEqual(imp.find_module('sys'), (None, 'sys', ('', '', 6))) self.assertEqual(imp.find_module('__builtin__'), (None, '__builtin__', ('', '', 6)))
async def render(self, path): name = os.path.basename(path) find = imp.find_module(name, [os.path.dirname(path)]) mod = imp.load_module(name, *find) return await mod.render(self.service, self.service.middleware)
def _import_module_from_library(module_name, path): # https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path file, path, description = imp.find_module(module_name, [path]) # Close the .so file after load. with file: return imp.load_module(module_name, file, path, description)
def load( self, data ): ''' Walk though a directory and load all the *py* and *pyc* modules ''' # use a set to insure that no two files are loaded twice names = Set() for directory in self.directories: for root, dirs, files in os.walk( directory ): for filename in files: # Get just the file name - no extension name, ext = os.path.splitext( filename ) if ( ext.lower() == ".py" and not name.lower().endswith( "_test" ) ): # and to names which will prevent doubles names.add( name ) self.logger.error( "name = {} ext = {}".format( name, ext ) ) for name in names: f = None filename = None description = None package = None continue_processing = True try: f, filename, description = imp.find_module( name, \ self.directories ) except ImportError as er: self.logger.error( "error finding {}: error is {}" \ .format( name, er ) ) continue_processing = False # Check if we failed. If so then jump to the beginning # and start next file if ( not continue_processing ): continue try: package = imp.load_module( name, f, filename, description ) except ImportError as ex: self.logger.error( "error importing {}: error is {}".\ format( name, ex ) ) continue_processing = False finally: if f: self.close_file( f ) # Check if we failed. If so then jump to the beginning # and start next file if ( not continue_processing ): continue try: # pprint(inspect.getmembers(package)) # pprint(inspect.getsourcelines(package.instantuate_me)) instance = package.instantuate_me( data ) if ( None != instance ): self.instances.append( instance ) self.logger.info( "Class {} instantiated".format( name ) ) except AttributeError as err: self.logger.error( \ "The function \"instantiate_me\" was not found in {}: {}"\ .format( name, err ) )
def import_module(self, mod_name, mod_path): ''' 动态调用第三方模块''' fn_, path, desc = imp.find_module(mod_name, [mod_path]) mod = imp.load_module(mod_name, fn_, path, desc) return mod
for arg in sys.argv: if arg == "-h" or arg == "--help": helpOpt = 1 print("\n####################################################### ") print("################## KOLKO I KRZYZYK ################### ") print("#######################################################\n") print( " Gracz rozpoczyna gre klikajac na jedno z dziewieciu\n pol na planszy.\n\n Gracz -> kolko, \n komputer -> krzyzyk.\n" ) print( "\n Program wymaga\n srodowiska graficznego oraz biblioteki PYGAME.\n" ) sys.exit(0) try: imp.find_module('pygame') import pygame from pygame.locals import * from pygame import gfxdraw except ImportError: print( ' # BLAD - brak biblioteki PyGame #\n Zainstaluj \'PyGame\' w celu poprawnego dzialania skryptu.\n' ) sys.exit(1) checkDisplay = os.popen("echo $DISPLAY") isGraphicalEnvExist = 1 if checkDisplay.read().strip() else 0 if not isGraphicalEnvExist: print(
# -*- coding: utf-8 -*- from . import qt from . import lang import os import json import imp from maya.app.general.mayaMixin import MayaQWidgetBaseMixin try: imp.find_module('PySide2') from PySide2.QtWidgets import * from PySide2.QtGui import * from PySide2.QtCore import * except ImportError: from PySide.QtGui import * from PySide.QtCore import * class Option(): def __init__(self): global WINDOW try: WINDOW.closeEvent(None) WINDOW.close() except Exception as e: print(e) WINDOW = SubWindow() #WINDOW.init_flag=False WINDOW.resize(800, 500) #WINDOW.move(WINDOW.pw-8, WINDOW.ph-31) WINDOW.show()
#!/usr/bin/python # coding=utf-8 from __future__ import print_function, unicode_literals import datetime, re, os, imp from bgmi.config import SCRIPT_PATH from bgmi.lib.fetch import DATA_SOURCE_MAP file, pathname, desc = imp.find_module('script_extend', [os.path.join(SCRIPT_PATH)]) imp.load_module('script_extend', file, pathname, desc) from script_extend.script import SearchScriptBase class Script(SearchScriptBase): class Model(SearchScriptBase.Model): bangumi_name = '普通攻擊是全體二連擊,這樣的媽媽你喜歡嗎?' cover = 'http://lain.bgm.tv/pic/cover/l/10/19/264355_PBB4E.jpg' update_time = 'Fri' # due_date = datetime.datetime(2019, 10, 1) source = 'dmhy' keyword = '普通攻击 喜欢吗' include_regex_filters = [ r'(BIG5|繁体|繁體|\[繁\])', ] exclude_regex_filters = [ r'(HEVC|MKV|H265)', ]
"""Unit tests for cloudformation.py""" import imp import os import sys module_name = 'provisioner' here_dir = os.path.dirname(os.path.abspath(__file__)) module_path = os.path.join(here_dir, '../../') sys.path.append(module_path) fp, pathname, description = imp.find_module(module_name) provisioner = imp.load_module(module_name, fp, pathname, description) class TestCloudFormation: def test_unit_cloudformation_get_stack_status(self): stack_id = "abc123" response = { "Stacks": [{ "StackName": "MyStack", "StackStatus": "There" }, { "StackName": "abc123", "StackStatus": "Here" }] } exists, status = provisioner.CloudFormation.get_stack_status( stack_id, response) assert exists is True assert status == "Here"
def __boot(): import sys import os PYTHONPATH = os.environ.get('PYTHONPATH') if PYTHONPATH is None or (sys.platform == 'win32' and not PYTHONPATH): PYTHONPATH = [] else: PYTHONPATH = PYTHONPATH.split(os.pathsep) pic = getattr(sys, 'path_importer_cache', {}) stdpath = sys.path[len(PYTHONPATH):] mydir = os.path.dirname(__file__) for item in stdpath: if item == mydir or not item: continue # skip if current dir. on Windows, or my own directory importer = pic.get(item) if importer is not None: loader = importer.find_module('site') if loader is not None: # This should actually reload the current module loader.load_module('site') break else: try: import imp # Avoid import loop in Python >= 3.3 stream, path, descr = imp.find_module('site', [item]) except ImportError: continue if stream is None: continue try: # This should actually reload the current module imp.load_module('site', stream, path, descr) finally: stream.close() break else: raise ImportError("Couldn't find the real 'site' module") known_paths = dict([(makepath(item)[1], 1) for item in sys.path]) # 2.2 comp oldpos = getattr(sys, '__egginsert', 0) # save old insertion position sys.__egginsert = 0 # and reset the current one for item in PYTHONPATH: addsitedir(item) sys.__egginsert += oldpos # restore effective old position d, nd = makepath(stdpath[0]) insert_at = None new_path = [] for item in sys.path: p, np = makepath(item) if np == nd and insert_at is None: # We've hit the first 'system' path entry, so added entries go here insert_at = len(new_path) if np in known_paths or insert_at is None: new_path.append(item) else: # new path after the insert point, back-insert it new_path.insert(insert_at, item) insert_at += 1 sys.path[:] = new_path
def readmodule_ex(module, path=[], inpackage=0): '''Read a module file and return a dictionary of classes. Search for MODULE in PATH and sys.path, read and parse the module and return a dictionary with one entry for each class found in the module.''' dict = {} i = string.rfind(module, '.') if i >= 0: # Dotted module name package = string.strip(module[:i]) submodule = string.strip(module[i + 1:]) parent = readmodule(package, path, inpackage) child = readmodule(submodule, parent['__path__'], 1) return child if _modules.has_key(module): # we've seen this module before... return _modules[module] if module in sys.builtin_module_names: # this is a built-in module _modules[module] = dict return dict # search the path for the module f = None if inpackage: try: f, file, (suff, mode, type) = \ imp.find_module(module, path) except ImportError: f = None if f is None: fullpath = list(path) + sys.path f, file, (suff, mode, type) = imp.find_module(module, fullpath) if type == imp.PKG_DIRECTORY: dict['__path__'] = [file] _modules[module] = dict path = [file] + path f, file, (suff, mode, type) = \ imp.find_module('__init__', [file]) if type != imp.PY_SOURCE: # not Python source, can't do anything with this module f.close() _modules[module] = dict return dict _modules[module] = dict imports = [] classstack = [] # stack of (class, indent) pairs src = f.read() f.close() # To avoid having to stop the regexp at each newline, instead # when we need a line number we simply string.count the number of # newlines in the string since the last time we did this; i.e., # lineno = lineno + \ # string.count(src, '\n', last_lineno_pos, here) # last_lineno_pos = here countnl = string.count lineno, last_lineno_pos = 1, 0 i = 0 while 1: m = _getnext(src, i) if not m: break start, i = m.span() if m.start("Method") >= 0: # found a method definition or function thisindent = _indent(m.group("MethodIndent")) meth_name = m.group("MethodName") lineno = lineno + \ countnl(src, '\n', last_lineno_pos, start) last_lineno_pos = start # close all classes indented at least as much while classstack and \ classstack[-1][1] >= thisindent: del classstack[-1] if classstack: # it's a class method cur_class = classstack[-1][0] cur_class._addmethod(meth_name, lineno) else: # it's a function f = Function(module, meth_name, file, lineno) dict[meth_name] = f elif m.start("String") >= 0: pass elif m.start("Class") >= 0: # we found a class definition thisindent = _indent(m.group("ClassIndent")) # close all classes indented at least as much while classstack and \ classstack[-1][1] >= thisindent: del classstack[-1] lineno = lineno + \ countnl(src, '\n', last_lineno_pos, start) last_lineno_pos = start class_name = m.group("ClassName") inherit = m.group("ClassSupers") if inherit: # the class inherits from other classes inherit = string.strip(inherit[1:-1]) names = [] for n in string.splitfields(inherit, ','): n = string.strip(n) if dict.has_key(n): # we know this super class n = dict[n] else: c = string.splitfields(n, '.') if len(c) > 1: # super class # is of the # form module.class: # look in # module for class m = c[-2] c = c[-1] if _modules.has_key(m): d = _modules[m] if d.has_key(c): n = d[c] names.append(n) inherit = names # remember this class cur_class = Class(module, class_name, inherit, file, lineno) dict[class_name] = cur_class classstack.append((cur_class, thisindent)) elif m.start("Import") >= 0: # import module for n in string.split(m.group("ImportList"), ','): n = string.strip(n) try: # recursively read the imported module d = readmodule(n, path, inpackage) except: ##print 'module', n, 'not found' pass elif m.start("ImportFrom") >= 0: # from module import stuff mod = m.group("ImportFromPath") names = string.split(m.group("ImportFromList"), ',') try: # recursively read the imported module d = readmodule(mod, path, inpackage) except: ##print 'module', mod, 'not found' continue # add any classes that were defined in the # imported module to our name space if they # were mentioned in the list for n in names: n = string.strip(n) if d.has_key(n): dict[n] = d[n] elif n == '*': # only add a name if not # already there (to mimic what # Python does internally) # also don't add names that # start with _ for n in d.keys(): if n[0] != '_' and \ not dict.has_key(n): dict[n] = d[n] else: assert 0, "regexp _getnext found something unexpected" return dict
from numpy import sum as numpysum from scipy.stats import norm from snippets.localfolder import get_path from src.parallelSIRs import ParallelSIRs import subprocess random.seed(17) MODEL = "locallevel" THISPATH = get_path() xmodulename = MODEL + "x" thetamodulename = MODEL + "theta" modelfolder = os.path.join(THISPATH, "models") sys.path.append(modelfolder) f, filename, description = imp.find_module(xmodulename) xmodule = imp.load_module("xmodule", f, filename, description) f, filename, description = imp.find_module(thetamodulename) thetamodule = imp.load_module("thetamodule", f, filename, description) print "Creating data set..." #xmodule.modelx.generateData(5000, xmodule.modelx.parameters, savefilename = "/tmp/txt.txt") syntheticdatasetpath = os.path.join(THISPATH, "data/%s-syntheticdata.R" % MODEL) if os.path.isfile(syntheticdatasetpath): xmodule.modelx.loadData(syntheticdatasetpath) xmodule.modelx.model_states = xmodule.modelx.model_states[:, newaxis] print xmodule.modelx.model_obs.shape nbparameters = thetamodule.modeltheta.parameterdimension Nx = 100 Ntheta = 1000
action="store", type="string", dest="dsFile", help="override dsFile") (options, args) = parser.parse_args() if not options.date: print "Date missing" sys.exit() if len(args) == 0: print "You should give the template file name" sys.exit() mod_dir, filename = os.path.split(args[0]) mod, ext = os.path.splitext(filename) f, filename, desc = imp.find_module(mod, [mod_dir]) mod = imp.load_module(mod, f, filename, desc) todo = [ "preamble", "dsFile", "anaType", "rootPath", "onTheFlyCustomization", "fun" ] for t in todo: globals()[t] = getattr(mod, t) if options.dsFile: print "Overriding dsFile to ", options.dsFile globals()["dsFile"] = options.dsFile dateTT = options.date #"20140411" ## TODO fixme! anaVersion = anaType + "_" + dateTT
def loadModules(self, path, base): modules = {} if not os.path.exists(path): log.debug("[SP Modules]: Error: Path doesn't exist: " + path) return # Import all subfolders to allow relative imports for root, dirs, files in os.walk(path): if root not in sys.path: sys.path.append(root) # List files files = [fname[:-3] for fname in os.listdir(path) if fname.endswith(".py") and not fname.startswith("__")] log.debug(files) if not files: files = [fname[:-4] for fname in os.listdir(path) if fname.endswith(".pyo") or fname.endswith(".pyc")] log.debug(files) # Import modules for name in files: module = None if name == "__init__": continue try: fp, pathname, description = imp.find_module(name, [path]) except Exception as e: log.debug("[SP Modules] Find module exception: " + str(e)) fp = None if not fp: log.debug("[SP Modules] No module found: " + str(name)) continue try: module = imp.load_module(name, fp, pathname, description) except Exception as e: log.debug("[SP Modules] Load exception: " + str(e)) finally: # Since we may exit via an exception, close fp explicitly. if fp: fp.close() if not module: log.debug("[SP Modules] No module available: " + str(name)) continue # Continue only if the attribute is available if not hasattr(module, name): log.debug("[SP Modules] Warning attribute not available: " + str(name)) continue # Continue only if attr is a class attr = getattr(module, name) if not inspect.isclass(attr): log.debug("[SeriesService] Warning no class definition: " + str(name)) continue # Continue only if the class is a subclass of the corresponding base class if not issubclass(attr, base): log.debug("[SP Modules] Warning no subclass of base: " + str(name)) continue # Add module to the module list modules[name] = attr return modules
from rucio.common.types import InternalAccount, InternalScope # delay import until function to avoid circular dependancy (note here for reference) # from rucio.core.rse import get_rse_name # Extra modules: Only imported if available EXTRA_MODULES = {'web': False, 'paramiko': False, 'flask': False} try: from rucio.db.sqla.enum import EnumSymbol EXTRA_MODULES['rucio.db.sqla.enum'] = True except ImportError: EXTRA_MODULES['rucio.db.sqla.enum'] = False for extra_module in EXTRA_MODULES: try: imp.find_module(extra_module) EXTRA_MODULES[extra_module] = True except ImportError: EXTRA_MODULES[extra_module] = False if EXTRA_MODULES['web']: from web import HTTPError if EXTRA_MODULES['paramiko']: try: from paramiko import RSAKey except Exception: EXTRA_MODULES['paramiko'] = False if EXTRA_MODULES['flask']: from flask import Response
def GetModule(self, ModuleName, ModuleFile, ModulePath): # Python fiddling to load a module from a file, there is probably a better way... f, Filename, desc = imp.find_module( ModuleFile.split('.')[0], [ModulePath]) # ModulePath = os.path.abspath(ModuleFile) return imp.load_module(ModuleName, f, Filename, desc)
def run(self, tmp=None, task_vars=None): play_context = copy.deepcopy(self._play_context) play_context.network_os = self._get_network_os(task_vars) # we should be able to stream line this a bit by creating a common # provider argument spec in module_utils/network_common.py or another # option is that there isn't a need to push provider into the module # since the connection is started in the action handler. f, p, d = find_module('ansible') f2, p2, d2 = find_module('module_utils', [p]) f3, p3, d3 = find_module(play_context.network_os, [p2]) module = load_module('ansible.module_utils.' + play_context.network_os, f3, p3, d3) self.provider = load_provider(module.get_provider_argspec(), self._task.args) if play_context.network_os == 'junos': play_context.connection = 'netconf' play_context.port = int(self.provider['port'] or self._play_context.port or 830) else: play_context.connection = 'network_cli' play_context.port = int(self.provider['port'] or self._play_context.port or 22) play_context.remote_addr = self.provider[ 'host'] or self._play_context.remote_addr play_context.remote_user = self.provider[ 'username'] or self._play_context.connection_user play_context.password = self.provider[ 'password'] or self._play_context.password play_context.private_key_file = self.provider[ 'ssh_keyfile'] or self._play_context.private_key_file play_context.timeout = int(self.provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT) if 'authorize' in self.provider.keys(): play_context.become = self.provider['authorize'] or False play_context.become_pass = self.provider['auth_pass'] if self._play_context.connection == 'local': socket_path = self._start_connection(play_context) task_vars['ansible_socket'] = socket_path if 'fail_on_missing_module' not in self._task.args: self._task.args['fail_on_missing_module'] = False result = super(ActionModule, self).run(tmp, task_vars) module = self._get_implementation_module(play_context.network_os, self._task.action) if not module: if self._task.args['fail_on_missing_module']: result['failed'] = True else: result['failed'] = False result['msg'] = ('Could not find implementation module %s for %s' % (self._task.action, play_context.network_os)) else: new_module_args = self._task.args.copy() # perhaps delete the provider argument here as well since the # module code doesn't need the information, the connection is # already started if 'network_os' in new_module_args: del new_module_args['network_os'] del new_module_args['fail_on_missing_module'] display.vvvv('Running implementation module %s' % module) result.update( self._execute_module(module_name=module, module_args=new_module_args, task_vars=task_vars, wrap_async=self._task. async)) display.vvvv('Caching network OS %s in facts' % play_context.network_os) result['ansible_facts'] = {'network_os': play_context.network_os} return result
import imp from imp_get_suffixes import module_types print 'Package:' f, filename, description = imp.find_module('example') print module_types[description[2]], filename print
def is_module_installed(module_name): try: imp.find_module(module_name) return True except ImportError: return False
def spy_imp_find_module(name, path): self.find_module_calls.append(name) return imp.find_module(name, path)
# Get the version try: __version__ = get_distribution("arm_pyart").version except DistributionNotFound: # package is not installed __version__ = '0.0.0' try: if _sys.version_info[:2] >= (3, 4): import importlib as _importlib specs = _importlib.util.find_spec('pytest') specs.loader.load_module() else: import imp as _imp _imp.find_module('pytest') except (AttributeError, ImportError) as error: def _test(verbose=False): """ This would invoke the Py-ART test suite, but pytest couldn't be imported so the test suite can not run. """ raise ImportError("Could not load pytest. Unit tests not available." " To run unit tests, please install pytest.") else: def _test(verbose=False): """ Invoke the Py-ART test suite. """
# Copyright 2015 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. """Tests for the Chromium Performance Dashboard data format implementation.""" import imp import os.path import sys import unittest try: imp.find_module("devtoolslib") except ImportError: sys.path.append(os.path.dirname(os.path.dirname( os.path.abspath(__file__)))) from devtoolslib.perf_dashboard import ChartDataRecorder class ChartDataRecorderTest(unittest.TestCase): """Tests the chart data recorder.""" def test_empty(self): """Tests chart data with no charts.""" recorder = ChartDataRecorder('benchmark') result = recorder.get_chart_data() self.assertEquals( { 'format_version': '1.0', 'benchmark_name': 'benchmark', 'charts': {} }, result)
def get_numpy_incpath(self): from imp import find_module # avoid actually importing numpy, it screws up distutils file, pathname, descr = find_module("numpy") from os.path import join return join(pathname, "core", "include")
} url = "https://chart.googleapis.com/chart?" + urlencode({ "chs":"200x200", "chld":"M|0", "cht":"qr", "chl":data }) return url ########NEW FILE######## __FILENAME__ = manage #!/usr/bin/env python from django.core.management import execute_manager import imp try: imp.find_module('settings') # Assumed to be in the same directory. except ImportError: import sys sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__) sys.exit(1) import settings if __name__ == "__main__": execute_manager(settings) ########NEW FILE######## __FILENAME__ = settings import os ROOT = os.path.dirname(os.path.realpath(__file__))
from main import app as application # # main(): # if __name__ == '__main__': application = imp.load_source('app', 'main.py') port = application.app.config['PORT'] ip = application.app.config['IP'] app_name = application.app.config['APP_NAME'] host_name = application.app.config['HOST_NAME'] fwtype = "wsgiref" for fw in ("gevent", "cherrypy", "flask"): try: imp.find_module(fw) fwtype = fw except ImportError: pass print('Starting WSGIServer type %s on %s:%d ... ' % (fwtype, ip, port)) if fwtype == "gevent": from gevent.pywsgi import WSGIServer WSGIServer((ip, port), application.app).serve_forever() elif fwtype == "cherrypy": from cherrypy import wsgiserver server = wsgiserver.CherryPyWSGIServer( (ip, port), application.app, server_name=host_name)
import re import shutil import sys try: import PyInstaller except ImportError: # if importing PyInstaller fails, try to load from parent # directory to support running without installation. import imp # Prevent running as superuser (root). if not hasattr(os, "getuid") or os.getuid() != 0: imp.load_module( 'PyInstaller', *imp.find_module( 'PyInstaller', [os.path.dirname(os.path.dirname(os.path.abspath(__file__)))])) from PyInstaller import HOMEPATH from PyInstaller import compat, configure from PyInstaller import main as pyi_main from PyInstaller.compat import is_py25, is_py26, is_win, is_darwin from PyInstaller.lib import unittest2 as unittest from PyInstaller.lib import junitxml from PyInstaller.utils import misc VERBOSE = False REPORT = False PYI_CONFIG = {} # Directory with this script (runtests.py). BASEDIR = os.path.dirname(os.path.abspath(__file__))
def __init__(self): # ## I have to do this in order to explicitly use ipv4 in instances where ipv6 is used but does not work. logging.info( 'HAVE TO DO THIS IN ORDER TO USE IPV4 WHEN IPV6 DOES NOT WORK') _old_getaddrinfo = socket.getaddrinfo def _new_getaddrinfo(*args, **kwargs): responses = _old_getaddrinfo(*args, **kwargs) return [ response for response in responses if response[0] == socket.AF_INET ] socket.getaddrinfo = _new_getaddrinfo if not os.environ.get( 'READTHEDOCS' ): # more and more contortions to get read the docs to work... # ## first see if we have PyQt4 try: val = imp.find_module('PyQt4') from PyQt4.QtGui import QFontDatabase except ImportError: print('ERROR, YOU NEED TO INSTALL PyQt4 ON YOUR MACHINE.') sys.exit(0) # ## now see if we have sshpass sshpass = find_executable('sshpass') if sshpass is None: print( 'ERROR, YOU NEED TO INSTALL sshpass ON YOUR MACHINE.') sys.exit(0) # ## now see if we have pandoc pandoc = find_executable('pandoc') if pandoc is None: print('ERROR, YOU NEED TO INSTALL pandoc ON YOUR MACHINE.') sys.exit(0) # ## first see if we have pip on this machine try: val = imp.find_module('pip') except ImportError: _choose_install_pip() # ## now go through all the dependencies in the requirements packages mainDir = os.path.dirname( os.path.dirname(os.path.abspath(__file__))) reqs = sorted( set( map( lambda line: line.replace('\n', '').strip(), open( os.path.join(mainDir, 'resources', 'requirements.txt'), 'r').readlines()))) reqs_remain = [] for req in reqs: try: val = imp.find_module(req) except ImportError: try: val = pkg_resources.require([req]) except pkg_resources.DistributionNotFound: reqs_remain.append(req) # if len(reqs_remain) != 0: _choose_install_local(reqs_remain)