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>If I understand the situation correctly, you're writing a module <code>wrapmod</code> that is intended to transform parts of an existing package <code>mod</code>. The specific part you're transforming is the submodule <code>mod.settings</code>. You've imported the <code>settings</code> module and made your changes to it, but even though it is available as <code>wrapmod.settings</code>, you can't use that module name in an <code>from ... import ...</code> statement.</p>

<p>I think the best way to fix that is to insert the modified module into <code>sys.modules</code> under the new dotted name. This makes Python accept that name as valid even though <code>wrapmod</code> isn't really a package.</p>

<p>So <code>wrapmod</code> would look something like:</p>

<pre><code>import sys

from mod import settings

# modify settings here

sys.modules['wrapmod.settings'] = settings   # add this line!
</code></pre>

python python-3.x python-3.4