Every line of 'python import from relative path' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.
4 def import_path(path): 5 path = os.path.abspath(path) 6 path, file = os.path.split(path) 7 file, ext = os.path.splitext(file) 8 sys.path.append(path) 9 module = __import__(file) 10 sys.path.pop() 11 return module
69 def _import_from(mod, path, mod_dir=None): 70 """ 71 Imports a module from a specific path 72 73 :param mod: 74 A unicode string of the module name 75 76 :param path: 77 A unicode string to the directory containing the module 78 79 :param mod_dir: 80 If the sub directory of "path" is different than the "mod" name, 81 pass the sub directory as a unicode string 82 83 :return: 84 None if not loaded, otherwise the module 85 """ 86 87 if mod_dir is None: 88 mod_dir = mod 89 90 if not os.path.exists(path): 91 return None 92 93 if not os.path.exists(os.path.join(path, mod_dir)): 94 return None 95 96 try: 97 mod_info = imp.find_module(mod_dir, [path]) 98 return imp.load_module(mod, *mod_info) 99 except ImportError: 100 return None
6 def import_by_path(path): 7 """ 8 Given a dotted/colon path, like project.module:ClassName.callable, 9 returns the object at the end of the path. 10 """ 11 module_path, object_path = path.split(":", 1) 12 target = importlib.import_module(module_path) 13 for bit in object_path.split("."): 14 target = getattr(target, bit) 15 return target
37 def import_class(from_path, full_class_name): 38 from_path = str(from_path) 39 full_class_name = str(full_class_name) 40 try: 41 return import_class_from_path(from_path, full_class_name) 42 except Exception as e: 43 our_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) 44 api_dir = os.path.join(our_dir, PULSAR_API_ROOT, PULSAR_FUNCTIONS_API_ROOT) 45 try: 46 return import_class_from_path(api_dir, full_class_name) 47 except Exception as e: 48 Log.info("Failed to import class %s from path %s" % (full_class_name, from_path)) 49 Log.info(e, exc_info=True) 50 return None
62 def import_or_alias(python_path: str) -> Union[Callable, str]: 63 """Import an object from a full python path or assume it is an alias for some object 64 in TensorFlow (e.g. "categorical_crossentropy"). 65 66 Args: 67 python_path: str, full python path or alias. 68 69 Returns: 70 callable/str, import object/alias. 71 72 Raises: 73 ImportError, object not found. 74 """ 75 python_path = _maybe_fix_tensorflow_python_path(python_path) 76 try: 77 module_path, obj_name = python_path.rsplit(".", 1) 78 except ValueError: 79 return python_path 80 81 module = importlib.import_module(module_path) 82 83 if not hasattr(module, obj_name): 84 raise ImportError(f"object: {obj_name} was not found in module {module_path}") 85 return getattr(module, obj_name)
241 def import_from_path(path, name="esptool"): 242 if not os.path.isfile(path): 243 raise Exception("No such file: %s" % path) 244 245 # Import esptool from the provided location 246 if sys.version_info >= (3,5): 247 import importlib.util 248 spec = importlib.util.spec_from_file_location(name, path) 249 module = importlib.util.module_from_spec(spec) 250 spec.loader.exec_module(module) 251 elif sys.version_info >= (3,3): 252 from importlib.machinery import SourceFileLoader 253 module = SourceFileLoader(name, path).load_module() 254 else: 255 import imp 256 module = imp.load_source(name, path) 257 return module
4 def import_dir(name, fromlist=()): 5 PACKAGE_EXT = '.sublime-package' 6 dirname = os.path.basename(os.path.dirname(os.path.realpath(__file__))) 7 if dirname.endswith(PACKAGE_EXT): 8 dirname = dirname[:-len(PACKAGE_EXT)] 9 return __import__('{0}.{1}'.format(dirname, name), fromlist=fromlist)
32 def import_module(path): 33 dirpath, mod_file = os.path.split(path) 34 mod_name = os.path.splitext(mod_file)[0] 35 full_name = os.path.splitext(path)[0] 36 #if os.path.exists(os.path.join(dirpath, '__init__.py')): 37 # dirpath = os.path.join(dirpath, '__init__.py') 38 39 if dirpath: 40 info = imp.find_module(mod_name, [dirpath]) 41 else: 42 info = imp.find_module(mod_name) 43 return imp.load_module(full_name, *info)
23 def import_from_directory(library, path): 24 """Import a library from a directory outside the path. 25 26 Parameters 27 ---------- 28 library: string 29 The name of the library. 30 path: string 31 The path of the folder containing the library. 32 33 """ 34 try: 35 return importlib.import_module(library) 36 except ImportError: 37 module_path = os.path.abspath(path) 38 version = sys.version_info 39 40 if version.major == 2: 41 f, filename, desc = imp.find_module(library, [module_path]) 42 return imp.load_module(library, f, filename, desc) 43 else: 44 sys.path.append(module_path) 45 return importlib.import_module(library)
111 def load_external_module(base: str, module: str) -> None: 112 current = os.path.dirname(os.path.abspath(base)) 113 module_dir = join(os.path.dirname(current), module) 114 sys.path.insert(0, module_dir)