Python import module from path | Example code
tutorial.eyehunts.com › python › python-importSep 27, 2021 · Using importlib Package. The importlib.util is one of the modules included in this package that can be used to import the module from the given path. import importlib.util spec = importlib.util.spec_from_file_location ("main", "modules/main.py") foo = importlib.util.module_from_spec (spec) spec.loader.exec_module (foo) print (foo.var) Output: Hello main file.
python - How to import a module given the full path? - Stack ...
stackoverflow.com › questions › 67631def import_module_from_file(full_path_to_module): """ Import a module given the full path/filename of the .py file Python 3.4 """ module = None try: # Get module name and path from full path module_dir, module_file = os.path.split(full_path_to_module) module_name, module_ext = os.path.splitext(module_file) # Get module "spec" from filename spec = importlib.util.spec_from_file_location(module_name,full_path_to_module) module = spec.loader.load_module() except Exception as ec: # Simple error ...