Python dynamic from?

59 views Asked by At

I was wondering if there exists a dynamic form of from in Python. Specifically because I want to use from 'project' import h, where project is a variable and h is a particular class instantiation. I know there's modulelib's import_module(), so I was wondering if there was something like that for from. Or would I just have to use import_module() and then call h from that?

1

There are 1 answers

1
sebastian On

Check __import__:

https://docs.python.org/2/library/functions.html#import

tmp = __import__('project', globals(), locals(), ['h'])
h = tmp.h

should be what you're looking for.

Which, looking at it, is not that much better than the import_module based solution...