3

I am working on a small library and I need to know can I import modules like numpy, sklearn and etc. Using functions. For example:

def ml():
    import numpy as np
    import pandas as pd
x = np.array([1,2,647,345,3,7,3,8,36,64])

Is this possible ? Simply can I import a module using a function and then use that later outside the function The main idea is when the user calls the function ml he has all the modules related to machine learning imported and then he can use them. X = np.array was just kind of an example.

1 Answer 1

2

UPDATED

This should work

import importlib
def importmd(modulex):
    return importlib.import_module(modulex) #Returning the module
np = importmd("numpy") #Same as import numpy as np
Sign up to request clarification or add additional context in comments.

7 Comments

Hi. What's the difference with just using import numpy in this situation? Thanks.
MasayoMusic just an example
Not Working GameDev
This is a working code, what's the error you're getting?
after pasting your code i typed x = numpy.array([1,2,3,4,4,5,5,5,4,3,2,]) I got numpy not defined
|

Your Answer

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