Home > Enterprise >  How to import a method from a library using the string name of the method in Python?
How to import a method from a library using the string name of the method in Python?

Time:06-16

I can do this:

from sklearn import metrics

How can I do this?

the_method='metrics'
from sklearn import the_method

CodePudding user response:

Mohamed's answer is right. I don't understand any reason where you would want to import a function to use that is stored as a string in a variable. Just simply do from sklearn import metrics . You could ask for an input and use an if statement to check the input and then call metrics() if appropriate.

CodePudding user response:

what do you want to achieve from this , i assume you only need to alias or customize the name ? if this is the case why not trying

from sklearn import metrics as the_method

  • Related