Pass-through/export whole third party module (using __all__?)
<p>I have a module that wraps another module to insert some shim logic in some functions. The wrapped module uses a settings module <code>mod.settings</code> which I want to expose, but I don't want the users to import it from there, in case I would like to shim something there as well in the future. I want them to import <code>wrapmod.settings</code>.</p>

<p>Importing the module and exporting it works, but is a bit verbose on the client side. It results in having to write <code>settings.thing</code> instead of just <code>thing</code>. </p>

<p>I want the users to be able to do <code>from wrapmod.settings import *</code> and get the same results as if they did <code>from mod.settings import *</code> but right now, only <code>from wrapmod import settings</code> is available. How to I work around this?</p>

<p>I ended up making a code-generator for a thin wrapper module instead, since the <code>sys.module</code> hacking broke all IDE integration. </p>

<pre><code>from ... import mod

# this is just a pass-through wrapper around mod.settings
__all__ = mod.__all__

# generate pass-through wrapper around mod.settings; doesn't break IDE integration, unlike manual sys.modules editing.
if __name__ == "__main__":
    for thing in settings.__all__:
        print(thing + " = mod." + thing)
</code></pre>

<p>which when run as a script, outputs code that can then be appended to the end of this file. </p>

python python-3.x python-3.4