Dynamically import specific names from a module vs importing full module
Ned Batchelder
ned at nedbatchelder.com
Mon Aug 22 16:50:55 EDT 2016
On Monday, August 22, 2016 at 4:22:09 PM UTC-4, Malcolm Greene wrote:
> Python 3.5: Is there a way to dynamically import specific names from a
> module vs importing the full module?
>
> By dynamic I mean via some form of importlib machinery, eg. I'm looking
> for the dynamic "from <module> import <name>" equivalent of "import
> <module>"'s importlib.import_module.
>
> Thank you,
> Malcolm
You can use:
the_mod = importlib.import_module("module")
a = the_mod.a
b = the_mod.b
# or
a = getattr(the_mod, 'a')
b = getattr(the_mod, 'b')
--Ned.
More information about the Python-list
mailing list