Created
September 24, 2020 13:06
-
-
Save mjm522/1298d92dcb4a624c59c83ec422f9d8eb to your computer and use it in GitHub Desktop.
Import a function from a python file specified in the arguments.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import argparse | |
import importlib | |
def import_function_handle(config_file): | |
file_path = [ROOT DIRECTORY WHERE YOUR PYTHON FILE EXIST]/{}.py' | |
file_path = file_path.format(config_file) | |
if not os.path.isfile(file_path): | |
print("\033[31;40mSuch a config file does not exist, check name ...\033[m") | |
print("\033[31;40mCurrent specified path: {}\033[m".format(file_path)) | |
exit(0) | |
spec = importlib.util.spec_from_file_location('config', file_path.format(config_file)) | |
spec_module = importlib.util.module_from_spec(spec) | |
sys.modules['config'] = spec_module | |
spec.loader.exec_module(spec_module) | |
from config import function_handle | |
return function_handle | |
parser.add_argument('--filename', type=str, default='somefile', help='check bic_policies/config') | |
args = parser.parse_args() | |
funtion_handle = import_function_handle(args.filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment