I have a string which contains the name of a library that I want to import. How can I import that library dynamically?
For example:
library = "mylibrary"
Morgan Thrapp
10k3 gold badges51 silver badges68 bronze badges
1 Answer 1
you can use python default function __import__ to import modules.
mod = __import__(module)
but path of this module need to be appended in sys.path
sys.path.insert(0, <path of module>)
answered Jul 14, 2016 at 14:02
Pradip Das
7381 gold badge7 silver badges16 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Morgan Thrapp
Best practice is to use
importlib.lang-py