I have structure:
modules
__init__.py
version_1.py
version_2.py
settings.py
my_script.py
__init__.py
In file version_1 and version_2 I have two versions the same function:
def ext_function(*args, **kwargs):
# something
I want to define in file settings.py what version have been loaded:
EXT_MODULE = 'modules.version_1'
and execute function in my_script like this:
settings.EXT_MODULE.ext_function(args)
How to do it? EXT_MODULE is a string so I can not execute function like this.
asked Mar 19, 2016 at 0:42
Nips
14k25 gold badges70 silver badges108 bronze badges
1 Answer 1
You can use the importlib module:
import importlib
module = importlib.import_module(settings.EXT_MODULE)
module.ext_function(args)
answered Mar 19, 2016 at 1:01
zondo
20.4k8 gold badges50 silver badges89 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py