Why does Python's __import__ require fromlist?
Posted
by ieure
on Stack Overflow
See other posts from Stack Overflow
or by ieure
Published on 2010-04-27T19:15:22Z
Indexed on
2010/04/27
19:23 UTC
Read the original article
Hit count: 255
In Python, if you want to programmatically import a module, you can do:
module = __import__('module_name')
If you want to import a submodule, you would think it would be a simple matter of:
module = __import__('module_name.submodule')
Of course, this doesn't work; you just get module_name
again. You have to do:
module = __import__('module_name.submodule', fromlist=['blah'])
Why? The actual value of fromlist
don't seem to matter at all, as long as it's non-empty. What is the point of requiring an argument, then ignoring its values?
Most stuff in Python seems to be done for good reason, but for the life of me, I can't come up with any reasonable explanation for this behavior to exist.
© Stack Overflow or respective owner