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.

asked Mar 19, 2016 at 0:42
0

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.