def find_module_path(module_name, search_path=None): """ Find the module path (pyd / so), while accounting for platform/arch naming :param module_name: The name of the module :param search_path: The path to search in. If None, searches system path. :return: The full path to the library or None if not found. """ # Use importlib if python 3.4+, else imp if sys.version_info[0] > 3 or (sys.version_info[0] == 3 and sys.version_info[1] >= 4): from importlib.machinery import FileFinder, ExtensionFileLoader, EXTENSION_SUFFIXES file_finder = FileFinder(search_path, (ExtensionFileLoader, EXTENSION_SUFFIXES)) # The search caches must be cleared to guaranteed find dynamically created modules file_finder.invalidate_caches() result = file_finder.find_spec(module_name) return None if not result else result.origin else: from imp import find_module # Deprecated in 3.4 try: result = find_module(module_name, [search_path]) except ImportError: result = None return None if not result else result[1]
def import_module(name, fullname=None): if fullname is None: fullname = 'trytond.modules.' + name try: module = importlib.import_module(fullname) except ImportError: if name not in EGG_MODULES: raise ep = EGG_MODULES[name] # Can not use ep.load because modules are declared in an importable # path and it can not import submodule. path = os.path.join(ep.dist.location, *ep.module_name.split('.')[:-1]) if not os.path.isdir(path): # Find module in path for path in sys.path: path = os.path.join(path, *ep.module_name.split('.')[:-1]) if os.path.isdir(os.path.join(path, name)): break else: # When testing modules from setuptools location is the # module directory path = os.path.dirname(ep.dist.location) spec = FileFinder( path, (SourceFileLoader, SOURCE_SUFFIXES)).find_spec(fullname) if spec.loader: module = spec.loader.load_module() else: raise return module
def _path_importer_cache(cls, path): try: finder = cls.path_importer_cache[path] except KeyError: finder = FileFinder(path, (PTLFileLoader, [PTL_EXT])) cls.path_importer_cache[path] = finder return finder
def available_players( player_paths: T.Optional[T.Sequence[str]] = None ) -> T.Sequence[ModuleInfo]: import othello module_paths = set(player_paths) if player_paths else set() othello_path: str = othello.__path__ # type: ignore # mypy issue #1422 package_paths = { *(path.join(p, "models", "players") for p in othello_path), *(dir_path for dir_path in module_paths if path.isdir(dir_path)), } module_paths -= package_paths return ( *walk_packages(package_paths), *( ModuleInfo( FileFinder( path.dirname(module_path), (SourceFileLoader, (".py", )), # type: ignore # typeshed is incorrect ), path.splitext(path.basename(module_path))[0], False, ) for module_path in module_paths if (path.isfile(module_path) and module_path.endswith(".py") and not path.basename(module_path).startswith("_"))), )
def load_module(name, path=None): finder = FileFinder(path, *_loader_details) spec = finder.find_spec(name) if not spec or not spec.loader: raise ImportError(f"no module named {name}") mod = module_from_spec(spec) spec.loader.exec_module(mod) return mod
def available_players(player_paths: T.Optional[T.Sequence[str]] = None) -> T.Sequence[ModuleInfo]: import othello module_path = othello.__path__ # type: ignore # mypy issue #1422 player_paths = player_paths or tuple() dir_player_paths = ( *(path.join(p, "models", "players") for p in module_path), *(dir_path for dir_path in player_paths if path.isdir(dir_path)), ) return ( *walk_packages(dir_player_paths, onerror=lambda _: None), *( ModuleInfo( FileFinder(path.dirname(file_path), (SourceFileLoader, (".py", ".pyc"))), path.splitext(path.basename(file_path))[0], False, ) for file_path in player_paths if path.isfile(file_path) ), )
def find_and_load_module(modulename, folder=""): finder = FileFinder(folder, loader_details) spec = finder.find_spec(modulename) module = module_from_spec(spec) spec.loader.exec_module(module) return module
def find_and_load_module(modulename, folder=""): finder = FileFinder(folder, loader_details) spec = finder.find_spec(modulename) return spec.loader.load_module()
def get_module_path(modulename, folder=""): finder = FileFinder(folder, loader_details) return finder.find_spec(modulename).origin
cmake_args.append(f"-DCUDA_TOOLKIT_ROOT_DIR:STRING={cuda_root}") elif dp_variant == "rocm": cmake_args.append("-DUSE_ROCM_TOOLKIT:BOOL=TRUE") rocm_root = os.environ.get("ROCM_ROOT") if rocm_root: cmake_args.append(f"-DROCM_ROOT:STRING={rocm_root}") else: raise RuntimeError("Unsupported DP_VARIANT option: %s" % dp_variant) # get tensorflow spec tf_spec = find_spec("tensorflow") if not tf_spec: # purelib gets site-packages path site_packages = get_path("purelib") if site_packages: tf_spec = FileFinder(site_packages).find_spec("tensorflow") # get install dir from spec try: tf_install_dir = tf_spec.submodule_search_locations[0] # type: ignore # AttributeError if ft_spec is None # TypeError if submodule_search_locations are None # IndexError if submodule_search_locations is an empty list except (AttributeError, TypeError, IndexError): setup_requires.extend(extras_require['cpu']) # setuptools will re-find tensorflow after installing setup_requires tf_install_dir = None # add cmake as a build requirement if cmake>3.7 is not installed try: cmake_version = get_cmake_version()
def _getFinder(self, path): try: return self._finderCache[path] except KeyError: self._finderCache[path] = FileFinder(path, (EPSLoader, ['.eps'])) return self._finderCache[path]