I have some modules like this:
Drivers/
a.py
b.py
c.py
Now I want to call them on the basis of a variable value. let us consider driver is the variable from where I will get the variable name.
if driver=='a':
#then call the driver a and execute.
a.somefunction()
if driver=='b':
#then call the driver b and execute
I know the value we get from the driver in if statement is a string type value and in the if statement we have to call a module. is there any way to convert it.??
If your "Drivers/" directory in the searchpath of python, simply import the module and call the function:
import importlib
module = importlib.import_module(driver)
module.some_function()
If the modules are in the same level(exactly your case), just
module = __import__(driver)
module.somefunction()
driver can be string such as 'a', 'b', or 'c'. If the module does not exist, ImportError is raised.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With