Home What is a pythonic way of specifying which class to import using config file and variable names

# What is a pythonic way of specifying which class to import using config file and variable names

user948
1#
user948 Published in March 18, 2018, 1:55 am
 I have written a JSON config file that lists all the inputs for running a package. That package, though, has several classes that are interchangeable. What is a pythonic way to define the imports from within the config file? I essentially want a cleaner way of doing this: JSON FILE: ... "import_location": "class_one_location", "import_name": "class_one", ...  PYTHON FILE: if config.import_name == 'class_one': from config.class_one_location import config.import_name as class if config.import_name == 'class_two': from config.class_two_location import config.import_name as class  As such, I can't do this because the import function doesn't accept variable names. I also don't believe I can use __import__ because I need to specify both the path and the module name within that path, although I may be misunderstanding. Thanks!
 You need to login account before you can post.
Processed in 0.295323 second(s) , Gzip On .