13 Apr 2016

Python import magic

Ever wondered how to make package export both its submodules for use with from package import * and re-export module members for use with from package import ClassName? Well, you can of course specify __all__ and then do from .module import * for each module, but that’s repeating yourself!

Instead, here’s a black magic (not really) technique which requires to specify each module only once.

# __init__.py
__all__ = [...]
from . import *

for module in __all__:
    m = globals()[module]
        all_attrs = m.__all__
    except AttributeError:
        all_attrs = [name for name in dir(m) if name[0] != '_']
    for name in all_attrs:
        globals()[name] = getattr(m, name)

Now, there are a lot of reasons not to use this. Somebody would argue that it’s a bad idea to re-export module members in package and that is correct in lots of cases. So only use this if you really know what are you doing and why.


New 0net-based comment system exists in a monad that haven't been unwrapped yet. Feel free to refer to refer to contacts page