Python 3 Examples: Import Another Python File as a Module ...
csatlas.com › python-import-file-moduleMar 25, 2021 · import importlib.machinery import importlib.util from pathlib import Path # Get path to mymodule script_dir = Path( __file__ ).parent mymodule_path = str( script_dir.joinpath( '..', 'alpha', 'beta', 'mymodule' ) ) # Import mymodule loader = importlib.machinery.SourceFileLoader( 'mymodule', mymodule_path ) spec = importlib.util.spec_from_loader( 'mymodule', loader ) mymodule = importlib.util.module_from_spec( spec ) loader.exec_module( mymodule ) # Use mymodule mymodule.say_hello()