0

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.

0

1 Answer 1

1

You can use the importlib module:

import importlib

module = importlib.import_module(settings.EXT_MODULE)
module.ext_function(args)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.