def test_sys_modules_loader_is_None(self): # If sys.modules[name].__loader__ is None, raise ValueError. name = 'some_mod' with util.uncache(name): module = imp.new_module(name) module.__loader__ = None sys.modules[name] = module with self.assertRaises(ValueError): importlib.find_loader(name)
def test_sys_modules_loader_is_not_set(self): # Should raise ValueError # Issue #17099 name = 'some_mod' with util.uncache(name): module = types.ModuleType(name) try: del module.__loader__ except AttributeError: pass sys.modules[name] = module with self.assertRaises(ValueError): importlib.find_loader(name)
def check_modules(self, module_name, module_fqn, module_path): with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') # imp.load_module will load even if it has already been loaded. # We need to ensure that happens in order to trigger the # deprecation warnings importlib.find_loader(module_fqn, module_path).load_module() warning_raised = False for warning in (e.message for e in w): if isinstance(warning, PendingDeprecationWarning) and \ ('%s is deprecated' % module_name) in warning.args[0]: warning_raised = True break tools.assert_true(warning_raised, msg='%s did not raise a PendingDeprecationWarning' % module_fqn)
def modules(self): # Returns True if all Python modules are downloaded # Returns a list of missing modules otherwise needed = [] for module in self._modules: try: importlib.find_loader(module) except: needed.append(module) # Return needed packages if len(needed) > 0: return needed else: return False
def test_success_path(self): # Searching on a path should work. name = "some_mod" path = "path to some place" with util.uncache(name): with util.import_state(meta_path=[self.FakeMetaFinder]): self.assertEqual((name, path), importlib.find_loader(name, path))
def __init__(self): self.session = requests.Session() self.session.headers.update( { 'Accept': 'application/json, application/xml', 'Accept-Language': 'en-US', 'User-Agent': 'Mozilla/5.0 GelbooruViewer/1.0 (+https://github.com/ArchieMeng/GelbooruViewer)' } ) # only cache for get_all with tags while pid is 0!!! if importlib.find_loader('lru'): from lru import LRU self.cache = LRU(GelbooruViewer.MAX_CACHE_SIZE) else: self.cache = dict() self.cache_lock = Lock() # occasionally update cache self.last_cache_used = time() self.update_cache_thread = Thread(target=self._update_cache_loop, daemon=True) self.update_cache_thread.start() # get latest image to update MAX_ID self.get(limit=0)
def create_app(): app = Flask(__name__, static_url_path="") app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://*****:*****@postgresql:5432/site' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['ALEMBIC'] = { 'script_location': 'migrations', 'version_locations': [] } database = SQLAlchemy(app) # подключение модулей skip = ['__pycache__'] for folder in os.listdir('./modules'): if os.path.isdir(os.path.join('./modules', folder)) and folder not in skip: # importing api api_path = os.path.join('./modules', folder, 'api') api_module = 'modules.' + folder + '.api' api = None if os.path.isdir(api_path): if find_loader(api_module): api = import_module(api_module) elif os.path.isfile(api_path + '.py'): api = import_module(api_module) if api is not None and hasattr(api, 'blueprint'): app.register_blueprint(api.blueprint) # importing migrations app.config['ALEMBIC']['version_locations'] = ( folder, 'modules/{}/migrations'.format(folder) ) return app
def _decode_base64_data( data_text: str, layer_width: int, compression: Optional[str] = None ) -> List[List[int]]: """Decode base64 data. Args: data_text (str): Data to be decoded. layer_width (int): Width of each layer in tiles. compression (Optional[str]): The type of compression for the data. Raises: ValueError: If compression type is unsupported. Returns: :List[List[int]]: Tile grid. """ tile_grid: List[List[int]] = [[]] unencoded_data = base64.b64decode(data_text) if compression == "zlib": unzipped_data = zlib.decompress(unencoded_data) elif compression == "gzip": unzipped_data = gzip.decompress(unencoded_data) elif compression == "zstd": modulename = 'zstandard' my_loader = importlib.find_loader(modulename) found = my_loader is not None if not found: raise ValueError("Can't load 'zstd' compressed map without the 'zstandard' " "library available. Either install 'zstandard' or go to " "Map Properties and change Tile Layer Format to " "Base64, Base64 gzip, or Base64.") else: import zstandard dctx = zstandard.ZstdDecompressor() unzipped_data = dctx.decompress(unencoded_data) elif compression is None: unzipped_data = unencoded_data else: raise ValueError(f"Unsupported compression type '{compression}'.") # Turn bytes into 4-byte integers byte_count = 0 int_count = 0 int_value = 0 row_count = 0 for byte in unzipped_data: int_value += byte << (byte_count * 8) byte_count += 1 if not byte_count % 4: byte_count = 0 int_count += 1 tile_grid[row_count].append(int_value) int_value = 0 if not int_count % layer_width: row_count += 1 tile_grid.append([]) tile_grid.pop() return tile_grid
def import_file(name, path): ''' Import source file with debugging <<< module = import_file(__file__, '/usr/local/lib/python3.7/dist-packages/denova/os/user.py') <<< print(str(module)) <module 'python.py' from '/usr/local/lib/python3.7/dist-packages/denova/os/user.py'> ''' import importlib try: log(f'import_file({path})') #DEBUG # deprecated in python 3.3 # the 'right' way to do this varies greatly with the specific python version # see http://stackoverflow.com/questions/19009932/import-arbitrary-python-source-file-python-3-3 # http://bugs.python.org/issue21436 # the following is undocumented in python 3, and may not work in all versions module = importlib.find_loader(name, path) log(f'import_file() result: {module}') #DEBUG except ImportError as imp_error: log(f'unable to import {path}') log('ImportError: ' + str(imp_error)) msg = f'could not import {path}' log(msg) raise ImportError(msg) return module
def copy(self, modname, target): loader = importlib.find_loader(modname, self.path) pkg = loader.is_package(modname) file = loader.get_filename(modname) if isinstance(loader, importlib.abc.FileLoader): if pkg: pkgdir, basename = os.path.split(file) assert basename.startswith('__init__') dest = os.path.join(target, modname) shutil.copytree(pkgdir, dest, ignore=shutil.ignore_patterns('*.pyc')) else: shutil.copy2(file, target) elif isinstance(loader, zipimport.zipimporter): prefix = loader.archive + '/' + loader.prefix assert file.startswith(prefix) path_in_zip = file[len(prefix):] zf = zipfile.ZipFile(loader.archive) if pkg: pkgdir, basename = path_in_zip.rsplit('/', 1) assert basename.startswith('__init__') pkgfiles = [f for f in zf.namelist() if f.startswith(pkgdir)] zf.extractall(target, pkgfiles) else: zf.extract(path_in_zip, target)
def _check_is_dataframe(obj): import importlib pandas_loader = importlib.find_loader('pandas') found = pandas_loader is not None if not found: return False import pandas as pd return isinstance(obj, pd.DataFrame)
def check_module_conditional_import(module_name): """ Test to see if we can import a module See: https://stackoverflow.com/questions/14050281/how-to-check-if-a-python-module-exists-without-importing-it Args: module_name (str): Module to be imported Returns: True if module is loadable """ if PY2: import imp try: imp.find_module(module_name) return True except ImportError: return False else: import importlib import importlib.util if not PY3_4_Plus: avail = importlib.find_loader(module_name) return avail is not None else: avail = importlib.util.find_spec(module_name) return avail is not None
def find_module_py33(string, path=None): loader = importlib.machinery.PathFinder.find_module(string, path) if loader is None and path is None: # Fallback to find builtins try: loader = importlib.find_loader(string) except ValueError as e: # See #491. Importlib might raise a ValueError, to avoid this, we # just raise an ImportError to fix the issue. raise ImportError("Originally ValueError: " + e.message) if loader is None: raise ImportError("Couldn't find a loader for {0}".format(string)) try: is_package = loader.is_package(string) if is_package: module_path = os.path.dirname(loader.path) module_file = None else: module_path = loader.get_filename(string) module_file = open(module_path, 'rb') except AttributeError: # ExtensionLoader has not attribute get_filename, instead it has a # path attribute that we can use to retrieve the module path try: module_path = loader.path module_file = open(loader.path, 'rb') except AttributeError: module_path = string module_file = None finally: is_package = False return module_file, module_path, is_package
def load_plugin_from_module(self, path): name = os.path.basename(path) module_loader = importlib.find_loader(name) module_spec = importlib.util.spec_from_loader(name, module_loader) module = importlib.util.module_from_spec(module_spec) module_spec.loader.exec_module(module) self.load_plugin_module(module)
def find_loader(fullname): """Find a PEP 302 "loader" object for fullname This is s convenience wrapper around :func:`importlib.find_loader` that sets the *path* argument correctly when searching for submodules, and also ensures parent packages (if any) are imported before searching for submodules. """ if fullname.startswith('.'): msg = "Relative module name {!r} not supported".format(fullname) raise ImportError(msg) path = None pkg_name = fullname.rpartition(".")[0] if pkg_name: pkg = importlib.import_module(pkg_name) path = getattr(pkg, "__path__", None) if path is None: return None try: return importlib.find_loader(fullname, path) except (ImportError, AttributeError, TypeError, ValueError) as ex: # This hack fixes an impedance mismatch between pkgutil and # importlib, where the latter throws other errors for cases where # pkgutil previously threw ImportError msg = "Error while finding loader for {!r} ({}: {})" raise ImportError(msg.format(fullname, type(ex), ex)) from ex
def find_module_py33(string, path=None): loader = importlib.machinery.PathFinder.find_module(string, path) if loader is None and path is None: # Fallback to find builtins loader = importlib.find_loader(string) if loader is None: raise ImportError("Couldn't find a loader for {0}".format(string)) try: is_package = loader.is_package(string) if is_package: module_path = os.path.dirname(loader.path) module_file = None else: module_path = loader.get_filename(string) mode = 'rb' if module_path.endswith(".pyc") else 'r' module_file = open(module_path, mode) except AttributeError: # ExtensionLoader has not attribute get_filename, instead it has a # path attribute that we can use to retrieve the module path try: module_path = loader.path module_file = open(loader.path, 'rb') except AttributeError: module_path = string module_file = None finally: is_package = False return module_file, module_path, is_package
def setup(self): """Download CLTK packages and trainer corpora. Launches the CLTK package download interface. Overridden by the CLTK child classes to launch the automated CLTK downloader. Convenience method if user has not already downloaded CLTK packages and trainer sets. Example: >>> LatinText('').setup() """ # check if cltk is already installed, if not, install it if not importlib.find_loader('cltk'): pip.main(['install', 'cltk']) # include cltk inline from cltk.corpus.utils.importer import CorpusImporter setup_language = self.options['language'] # for ancient greek, change to 'greek' for purposes of cltk setup if setup_language == 'ancient greek': setup_language = 'greek' corpus_importer = CorpusImporter(setup_language) # loop through, check if extant, attempt to download, skip any errors for cltk_corpus in corpus_importer.list_corpora: print('Downloading', cltk_corpus) try: corpus_importer.import_corpus(cltk_corpus) except: print('Problem downloading', cltk_corpus, '(skipping)') return True
def load_module(name, package, path): """Load a module/package. Returns the module or None. Doesn't catch any exceptions during the actual import. """ fullname = package + "." + name try: return sys.modules[fullname] except KeyError: pass loader = importlib.find_loader(fullname, [path]) if loader is None: return # modules need a parent package if package not in sys.modules: spec = importlib.machinery.ModuleSpec(package, None, is_package=True) sys.modules[package] = importlib.util.module_from_spec(spec) mod = loader.load_module(fullname) # make it accessible from the parent, like __import__ does vars(sys.modules[package])[name] = mod return mod
def find_module_py33(string, path=None): loader = importlib.machinery.PathFinder.find_module(string, path) if loader is None and path is None: # Fallback to find builtins loader = importlib.find_loader(string) if loader is None: raise ImportError("Couldn't find a loader for {0}".format(string)) try: is_package = loader.is_package(string) if is_package: module_path = os.path.dirname(loader.path) module_file = None else: module_path = loader.get_filename(string) module_file = open(module_path, 'rb') except AttributeError: # ExtensionLoader has not attribute get_filename, instead it has a # path attribute that we can use to retrieve the module path try: module_path = loader.path module_file = open(loader.path, 'rb') except AttributeError: module_path = string module_file = None finally: is_package = False return module_file, module_path, is_package
def verify_package(): is_2 = 2 <= gVersion < 3 has_matplot = False if is_2: import imp try: imp.find_module("matplotlib") has_matplot = True except ImportError: has_matplot = False elif gVersion >= 3.4: import importlib.util check = importlib.util.find_spec("matplotlib") has_matplot = check is not None elif not is_2 and gVersion < 3.4: import importlib spam_loader = importlib.find_loader("matplotlib") has_matplot = spam_loader is not None else: print("Python version not compatable.") exit(ENTAP_EXIT_UNSUPPORTED_SOFTWARE) if has_matplot: global gPlot import matplotlib import matplotlib.pyplot as gPlot gPlot.switch_backend('agg') else: print("Matplotlib module not found. Not able to graph data.") exit(ENTAP_EXIT_UNSUPPORTED_SOFTWARE)
def install_jet(): import importlib if importlib.find_loader('pycurl') == None: print(color_red("Not Found : ") + "pycurl") print(color_yellow("Installing : ") + "pycurl") os.system("pip3 install pycurl")
def copy(self, modname, target): """Copy the importable module 'modname' to the directory 'target'. modname should be a top-level import, i.e. without any dots. Packages are always copied whole. This can currently copy regular filesystem files and directories, and extract modules and packages from appropriately structured zip files. """ loader = importlib.find_loader(modname, self.path) if loader is None: raise ImportError('Could not find %s' % modname) pkg = loader.is_package(modname) if isinstance(loader, importlib.machinery.ExtensionFileLoader): check_ext_mod(loader.path, self.py_version) shutil.copy2(loader.path, target) elif isinstance(loader, importlib.abc.FileLoader): file = loader.get_filename(modname) if pkg: pkgdir, basename = os.path.split(file) assert basename.startswith('__init__') check_package_for_ext_mods(pkgdir, self.py_version) dest = os.path.join(target, modname) shutil.copytree(pkgdir, dest, ignore=shutil.ignore_patterns('*.pyc')) else: shutil.copy2(file, target) elif isinstance(loader, zipimport.zipimporter): copy_zipmodule(loader, modname, target)
def find_module_py33(string, path=None): mod_info = (None, None, None) loader = None if path is not None: # Check for the module in the specidied path loader = importlib.machinery.PathFinder.find_module(string, path) else: # Check for the module in sys.path loader = importlib.machinery.PathFinder.find_module(string, sys.path) if loader is None: # Fallback to find builtins loader = importlib.find_loader(string) if loader is None: raise ImportError try: if (loader.is_package(string)): mod_info = (None, os.path.dirname(loader.path), True) else: filename = loader.get_filename(string) if filename and os.path.exists(filename): mod_info = (open(filename, 'U'), filename, False) else: mod_info = (None, filename, False) except AttributeError: mod_info = (None, loader.load_module(string).__name__, False) return mod_info
def find_module_py33(string, path=None, loader=None, full_name=None, is_global_search=True): loader = loader or importlib.machinery.PathFinder.find_module(string, path) if loader is None and path is None: # Fallback to find builtins try: with warnings.catch_warnings(record=True): # Mute "DeprecationWarning: Use importlib.util.find_spec() # instead." While we should replace that in the future, it's # probably good to wait until we deprecate Python 3.3, since # it was added in Python 3.4 and find_loader hasn't been # removed in 3.6. loader = importlib.find_loader(string) except ValueError as e: # See #491. Importlib might raise a ValueError, to avoid this, we # just raise an ImportError to fix the issue. raise ImportError("Originally " + repr(e)) if loader is None: raise ImportError("Couldn't find a loader for {}".format(string)) return _from_loader(loader, string)
def find_loader(fullname): """Find a PEP 302 "loader" object for fullname This is s convenience wrapper around :func:`importlib.find_loader` that sets the *path* argument correctly when searching for submodules, and also ensures parent packages (if any) are imported before searching for submodules. """ if fullname.startswith('.'): msg = "Relative module name {!r} not supported".format(fullname) raise ImportError(msg) path = None pkg_name = fullname.rpartition(".")[0] if pkg_name: pkg = importlib.import_module(pkg_name) path = getattr(pkg, "__path__", None) if path is None: return None try: return importlib.find_loader(fullname, path) except (ImportError, AttributeError, TypeError, ValueError) as ex: # This hack fixes an impedance mismatch between pkgutil and # importlib, where the latter raises other errors for cases where # pkgutil previously raised ImportError msg = "Error while finding loader for {!r} ({}: {})" raise ImportError(msg.format(fullname, type(ex), ex)) from ex
def ssl(module, host='127.0.0.1', path=None): """ Adds SSL support for development :param module: str Name of the module :param host: str Domain or IP of the server Default = '127.0.0.1' :param path: str Path to cert files (without .crt and .key ending) If empty you have to install python OpenSSL lib Default = None """ state_ = state(module) if path: state_.ssl = make_ssl_devcert(path, host=host) else: import importlib # noinspection PyDeprecation open_ssl = importlib.find_loader('OpenSSL') if open_ssl: state_.ssl = 'adhoc' else: raise ModuleNotFoundError( 'SSL generation requires the PyOpenSSl module. Please install it or pass the path\ to your self generated certificate')
def setup_cost_calculator(self): cost_calc_widget = QWidget() self.cost_calc_layout = QFormLayout() self.raster_layer_combo_box = QgsMapLayerComboBox() self.raster_layer_combo_box.setCurrentIndex(-1) self.raster_layer_combo_box.setFilters( QgsMapLayerProxyModel.RasterLayer) self.raster_layer_combo_box.currentIndexChanged.connect( self.updateCostName) self.costs = [] self.cost_calc_layout.addRow("Reference Raster: ", self.raster_layer_combo_box) #self.addCost('_lightness','Lightness') self.addCost('_darkness', 'Darkness') self.addCost('_sobel', 'Sobel') self.addCost('_sobv', 'Sobel Vertical Transform') self.addCost('_sobh', 'Sobel Horizontal Transform') self.addCost('_roberts', 'Roberts\' cross opperator') self.addCost('_prewitt', 'Prewitt Transform') self.addCost('_scharr', 'Scharr transform') self.addCost('_phase', 'Phase Congruency') if importlib.find_loader('phasepack') is None: self.costs[-1][1].setEnabled(False) self.cost_name = QLineEdit() self.cost_calc_layout.addRow("Cost Layer Name", self.cost_name) cost_calculator_run = QPushButton("Run") self.cost_calc_layout.addRow(cost_calculator_run) cost_calc_widget.setLayout(self.cost_calc_layout) cost_calculator_run.clicked.connect(self.run_costcalculator) return cost_calc_widget
def module_exists(name): if sys.version_info[0] == 3: if sys.version_info[1] <= 4: return importlib.find_loader(name) is not None else: return importlib.util.find_spec(name) is not None return False
def is_module_available(name): """Проверяет доступность модуля для импорта, при этом не импортируя его. :param str name: имя модуля :rtype: bool """ if PY2 and isinstance(name, text): name = name.encode('utf-8') elif not isinstance(name, text): name = name.decode('utf-8') if PY2: import imp path = None try: for x in name.split('.'): if path is not None: path = [path] path = imp.find_module(x, path)[1] return True except ImportError: return False elif sys.version_info < (3, 4): import importlib return importlib.find_loader(name) is not None else: # >= 3.4 import importlib.util return importlib.util.find_spec(name) is not None
def setup_webdriver(test=False): # selenium package 설치 if not importlib.find_loader('selenium'): pip.main(['install', 'selenium']) print('chromedriver 다운로드') downloaded_file = download_chromedriver() print(downloaded_file) # 압축 해제 print('다운로드 받은 파일 압축 해제 ...') driverzip = zipfile.ZipFile(downloaded_file) driverzip.extractall() driverfile = driverzip.namelist()[0] driverzip.close() # 실행권한 설정 st = os.stat(driverfile) os.chmod(driverfile, st.st_mode | stat.S_IEXEC) if test: print('설정 테스트 ...', end=' ') test_webdriver(driverfile) print('완료') return driverfile
def detect(name): assert name in names d = {'name': name} if name in builtins: d['type'] = 'builtin' return d if name in python_modules: loader = importlib.find_loader(python_modules[name]) if loader is not None: d['type'] = 'python' d['module'] = python_modules[name] d['path'] = loader.get_filename() return d envvar = get_executable_env_var(name) if envvar in os.environ: d['command'] = os.environ[envvar] d['envvar'] = envvar d['type'] = 'environment' return d if name in default_executables: commands = default_executables[name] for command in commands: fullpath = shutil.which(command) if fullpath: d['command'] = command d['fullpath'] = fullpath d['type'] = 'which' return d
def plugin_item_changed(self, index): """ This function is called when the user selects another plugin in the plugin tree on the left side. Every change of plugin updates the displayed plugin information on the right side. :param index: :return: """ plugin_info = self.pluginTree.model().data(index, Qt.UserRole) self.clear() self.scrollArea.setDisabled(True) if plugin_info is None: return self.scrollArea.setDisabled(False) self.nameEdit.setText(plugin_info.name) self.authorEdit.setText(plugin_info.author) self.descriptionText.setText(plugin_info.description) self.pathEdit.setText(plugin_info.path) self.createButton.setEnabled(plugin_info.loadable) lines = None with open(plugin_info.path + '.py') as f: lines = f.readlines() found_imports = [] for line in lines: if line.startswith('import'): m = re.search('(import)\s+([\w.]*)(\s+as){0,1}', str.strip(line)) if m is not None: if len(m.groups()) > 2: found_imports.append(m.group(2)) if line.startswith('from'): m = re.search('(from)\s+([\w.]*)(\s+import)', str.strip(line)) if m is not None: if len(m.groups()) > 2: found_imports.append(m.group(2)) found_imports.sort() for imp in found_imports: item = QListWidgetItem(imp) spam_loader = importlib.find_loader(imp) found = spam_loader is not None if not found: self.modulesList.addItem(item) item.setBackground(QColor(255, 0, 0, 50)) if not plugin_info.loadable: self.modulesList.setEnabled(True) self.modulesLabel.setEnabled(True)
def ensure_pip(): """ Validate pip is installed so we can install packages on demand. """ if importlib.find_loader('pip') is None: print("Your Python installation did not bundle 'pip'") print("Home Assistant requires 'pip' to be installed.") print("Please install pip: " "https://pip.pypa.io/en/latest/installing.html") sys.exit()
def check_modules(self, module_name, module_fqn, module_path): with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') # imp.load_module will load even if it has already been loaded. # We need to ensure that happens in order to trigger the # deprecation warnings importlib.find_loader(module_fqn, module_path).load_module() warning_raised = False for warning in (e.message for e in w): if isinstance(warning, PendingDeprecationWarning) and \ ('%s is deprecated' % module_name) in warning.args[0]: warning_raised = True break tools.assert_true( warning_raised, msg='%s did not raise a PendingDeprecationWarning' % module_fqn)
def load_handlers(self): modules = [ entry.split('.')[0] for entry in os.listdir("handlers") if os.path.isfile(os.path.join("handlers", entry)) ] loaders = {module: importlib.find_loader(module, ["handlers"]) for module in modules} self.handlers = [loader.load_module(name) for name, loader in loaders.items() if loader]
def install_package(package): if not importlib.find_loader(package): import pip pip.main(["install", package]) # reload site paths so we can later import the package import site importlib.reload(site)
def test_success_path(self): # Searching on a path should work. name = 'some_mod' path = 'path to some place' with util.uncache(name): with util.import_state(meta_path=[self.FakeMetaFinder]): self.assertEqual((name, path), importlib.find_loader(name, path))
def __init__(self, **kwargs): APIProvider.__init__(self, **kwargs) self.cache_file = get_cache_file(self.id_) # Use lxml as the BeautifulSoup parser if it's installed. if importlib.find_loader('lxml'): self._parser = 'lxml' else: self._parser = 'html.parser'
def import_in_folder(path, name): try: del sys.modules[name] # force path search except KeyError: pass loader = importlib.find_loader(name, [path]) if loader is None: raise ImportError("Could not find loader") return loader.load_module()
def _importable(module_str): """check whether module can be imported""" try: l = importlib.find_loader(module_str) found = l is not None except Exception: logger.error("Could not import module: '{}'".format(module_str)) raise ImportError return found
def test_sys_modules(self): # If a module with __loader__ is in sys.modules, then return it. name = 'some_mod' with util.uncache(name): module = imp.new_module(name) loader = 'a loader!' module.__loader__ = loader sys.modules[name] = module found = importlib.find_loader(name) self.assertEqual(loader, found)
def find_module(self, fullname, path=None): # If the module being imported is not one we have registered # post import hooks for, we can return immediately. We will # take no further part in the importing of this module. if not fullname in _post_import_hooks: return None # When we are interested in a specific module, we will call back # into the import system a second time to defer to the import # finder that is supposed to handle the importing of the module. # We set an in progress flag for the target module so that on # the second time through we don't trigger another call back # into the import system and cause a infinite loop. if fullname in self.in_progress: return None self.in_progress[fullname] = True # Now call back into the import system again. try: if PY3: # For Python 3 we need to use find_spec().loader # from the importlib.util module. It doesn't actually # import the target module and only finds the # loader. If a loader is found, we need to return # our own loader which will then in turn call the # real loader to import the module and invoke the # post import hooks. try: import importlib.util loader = importlib.util.find_spec(fullname).loader except (ImportError, AttributeError): loader = importlib.find_loader(fullname, path) if loader: return _ImportHookChainedLoader(loader) else: # For Python 2 we don't have much choice but to # call back in to __import__(). This will # actually cause the module to be imported. If no # module could be found then ImportError will be # raised. Otherwise we return a loader which # returns the already loaded module and invokes # the post import hooks. __import__(fullname) return _ImportHookLoader() finally: del self.in_progress[fullname]
def check_module_presence(module_name): is_module_present = True try: if importlib.find_loader(module_name) is None: is_module_present = False except ValueError: is_module_present = False return is_module_present
def load_module(module_name, module_path): """Load the module named `module_name` from `module_path` independently of the Python version.""" if hasattr(importlib, 'find_loader'): # Python 3 loader = importlib.find_loader(module_name, [module_path]) return loader.load_module() else: # Python 2.7 module_info = imp.find_module(module_name, [module_path]) return imp.load_module(module_name, *module_info)
def _find_and_load(self, name): """Find and load the module. Inserts the module into self.modules and returns it. If the module is not found or could not be imported, it is inserted in self.badmodules. """ path = self.path parent = name.rpartition('.')[0] if parent: if parent not in self.modules: self._gcd_import(parent) # Crazy side-effects! if name in self.modules: return self.modules[name] # Backwards-compatibility; be nicer to skip the dict lookup. parent_module = self.modules[parent] try: path = parent_module.__path__ except AttributeError: # this fixes 'import os.path'. Does it create other problems? child = name.rpartition('.')[2] if child in parent_module.__globalnames__: return parent_module msg = ('No module named {!r}; {} is not a package').format(name, parent) self._add_badmodule(name) raise ImportError(msg, name=name) loader = importlib.find_loader(name, path) if loader is None: self._add_badmodule(name) raise ImportError(name) elif name not in self.modules: # The parent import may have already imported this module. try: self._load_module(loader, name) except ImportError: self._add_badmodule(name) raise # Backwards-compatibility; be nicer to skip the dict lookup. module = self.modules[name] if parent: # Set the module as an attribute on its parent. parent_module = self.modules[parent] setattr(parent_module, name.rpartition('.')[2], module) # It is important that all the required __...__ attributes at # the module are set before the code is scanned. if module.__code__: self._scan_code(module.__code__, module) return module
def _misoc_import(default, external, name): if external: try: del sys.modules[name] # force external path search except KeyError: pass loader = importlib.find_loader(name, [external]) if loader is None: raise ImportError("Module not found: "+name) return loader.load_module() else: return importlib.import_module(default + "." + name)
def is_installed(self, module): if hasattr(importlib, 'find_loader'): found = importlib.find_loader(module) else: try: found = imp.find_module(module) except ImportError: found = False # Coerce result to boolean if found: return True else: return False
def has_module(module_name): """Check to see if a python module is available. """ if sys.version_info > (3, ): import importlib return importlib.find_loader(module_name) is not None else: # pragma: no cover import imp try: imp.find_module(module_name) except ImportError: return False return True
def find_module_py33(string, path=None, loader=None, full_name=None): loader = loader or importlib.machinery.PathFinder.find_module(string, path) if loader is None and path is None: # Fallback to find builtins try: with warnings.catch_warnings(record=True): # Mute "DeprecationWarning: Use importlib.util.find_spec() # instead." While we should replace that in the future, it's # probably good to wait until we deprecate Python 3.3, since # it was added in Python 3.4 and find_loader hasn't been # removed in 3.6. loader = importlib.find_loader(string) except ValueError as e: # See #491. Importlib might raise a ValueError, to avoid this, we # just raise an ImportError to fix the issue. raise ImportError("Originally " + repr(e)) if loader is None: raise ImportError("Couldn't find a loader for {}".format(string)) try: is_package = loader.is_package(string) if is_package: if hasattr(loader, 'path'): module_path = os.path.dirname(loader.path) else: # At least zipimporter does not have path attribute module_path = os.path.dirname(loader.get_filename(string)) if hasattr(loader, 'archive'): module_file = DummyFile(loader, string) else: module_file = None else: module_path = loader.get_filename(string) module_file = DummyFile(loader, string) except AttributeError: # ExtensionLoader has not attribute get_filename, instead it has a # path attribute that we can use to retrieve the module path try: module_path = loader.path module_file = DummyFile(loader, string) except AttributeError: module_path = string module_file = None finally: is_package = False if hasattr(loader, 'archive'): module_path = loader.archive return module_file, module_path, is_package
def copy_module(name, destination): """Copy the importable module 'name' to the 'destination' directory""" loader = importlib.find_loader(name) if not isinstance(loader, importlib.abc.FileLoader): print('Loader for module %s is not handled', name) sys.exit(1) print('Copying "%s" to "%s"' % (name, destination)) filename = loader.get_filename(name) if loader.is_package(name): pkgdir, _ = os.path.split(filename) shutil.copytree(pkgdir, os.path.join(destination, name)) else: shutil.copy2(filename, destination)
def stats(path): stats = dict() from importlib import find_loader if os.path.isfile(path): stat_result = os.stat(path) stats['birth'] = dt.fromtimestamp(stat_result.st_birthtime).timetuple() stats['mtime'] = dt.fromtimestamp(stat_result.st_mtime).timetuple() stats['ctime'] = dt.fromtimestamp(stat_result.st_ctime).timetuple() stats['atime'] = dt.fromtimestamp(stat_result.st_atime).timetuple() else: stat_result = os.stat(find_loader(name).path) stats['birth'] = dt.fromtimestamp(stat_result.st_birthtime).timetuple() stats['mtime'] = dt.fromtimestamp(stat_result.st_mtime).timetuple() stats['ctime'] = dt.fromtimestamp(stat_result.st_ctime).timetuple() stats['atime'] = dt.fromtimestamp(stat_result.st_atime).timetuple() return stats
def find_loader(fullname): if fullname.startswith('.'): msg = 'Relative module name {!r} not supported'.format(fullname) raise ImportError(msg) path = None pkg_name = fullname.rpartition('.')[0] if pkg_name: pkg = importlib.import_module(pkg_name) path = getattr(pkg, '__path__', None) if path is None: return try: return importlib.find_loader(fullname, path) except (ImportError, AttributeError, TypeError, ValueError) as ex: msg = 'Error while finding loader for {!r} ({}: {})' raise ImportError(msg.format(fullname, type(ex), ex)) from ex
def plugin_loaded(): global wsh import importlib whoosh_libdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'whoosh_2_5_4') wsh_loader = importlib.find_loader('whoosh', [whoosh_libdir]) wsh = wsh_loader.load_module('whoosh') import whoosh.index import whoosh.fields import whoosh.qparser import whoosh.query global SCHEMA SCHEMA = wsh.fields.Schema(path=wsh.fields.ID(stored=True), mtime=wsh.fields.STORED, fsize=wsh.fields.STORED, data=wsh.fields.NGRAM(stored=False, phrase=True, minsize=2, maxsize=2)) Const.cache_ix = open_ix(load_index_dir(), '__Searchlime_cache__', create=True, schema=wsh.fields.Schema(name=wsh.fields.ID(stored=True), tree=wsh.fields.STORED) )