0

Is it possible to hand over a library import in Python to a subroutiune?

I got the problem, that the import of a certain matplotlib version took very long. To see where it comes from I did for each library import the following:

timer = time.time()
import theLibToImport as tlti
print("import theLibToImport took %.3f sec" % (time.time()-timer))

I would prefer something like this

tldi = myImport("theLibToImport")

def myImport(theLib: str):

    timer = time.time()    
    import theLib as shortName # some Python woodoo to transfer string to module
    print("import %s took %.3f sec" % (theLib,time.time()-timer))

    return shortName

The library should be globally available in the py file. Is this possible in a comparable way?

Michael Hecht
  • 1,813
  • 5
  • 20
  • 31
  • does [this](https://stackoverflow.com/questions/8718885/import-module-from-string-variable) help, the first answer mentions use of `importlib`. – typedecker Jan 26 '22 at 14:38

0 Answers0