Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def _import_module(file_path):
- if not os.path.exists(file_path):
- raise ImportError
- module_name = os.path.splitext(os.path.basename(file_path))[0]
- module_dir = os.path.dirname(file_path)
- # Ugly inconsistency: Python will insist on correctly cased module names
- # independent of whether the OS is case-sensitive or not.
- # We want all cases to match though.
- if platform.system() == 'Windows': # pragma: nocover
- for cased_file_path in os.listdir(module_dir):
- cased_module_name = os.path.splitext(cased_file_path)[0]
- if cased_module_name.lower() == module_name.lower():
- module_name = cased_module_name
- break
- if sys.version_info < (3, 5):
- from importlib.machinery import SourceFileLoader
- module = SourceFileLoader(module_name, file_path).load_module()
- else:
- import importlib.util
- spec = importlib.util.spec_from_file_location(module_name, file_path)
- module = importlib.util.module_from_spec(spec)
- spec.loader.exec_module(module)
- return module
Add Comment
Please, Sign In to add comment