I wanna something like that :
import Variant
x = Variant.Pop1
from x import mid
is this possible?
To import a module from a name in a variable, use importlib (see python docs)
For example:
import importlib
import os.path as p
x = p.__name__
q = importlib.import_module(x)
Of course, this example is silly because q ends up as with the same value as p, but it demonstrates the syntax. Please provide more detail if this is not what you want.
Another option is to use exec, as in this other post
It is, but this is not a good example of how to import modules, it's a bad practice.
This is exactly what you want to do, but better.
from Variant.Pop1 import mid as x
def my_func(var):
# do something
my_func(x)
Example:
from random import choice as x
my_list = [1, 2, 3, 4]
def my_func(var):
return var(my_list)
print(my_func(x))
from Variant.Pop1 import mid?from Variant.Pop1 import mid as pop1_mid,from Variant.Pop2 import mid as pop2_midand so on.