Python namespace change of `import package.sub_module; from package import *`
<p>The <a href="https://docs.python.org/3/tutorial/modules.html#importing-from-a-package" rel="nofollow">Python documentation</a> says</p>

<blockquote>
  <p>Consider this code:</p>
</blockquote>

<pre><code>import sound.effects.echo
import sound.effects.surround
from sound.effects import *
</code></pre>

<blockquote>
  <p>In this example, the echo and surround modules are imported in the
  current namespace because they are defined in the sound.effects
  package when the from...import statement is executed. <strong>(This also works
  when __all__ is defined.)</strong></p>
</blockquote>

<p>I try the following code</p>

<pre><code># package/
#     __init__.py
#     sub_module.py

import package.sub_module
from package import *
print(sub_module)
</code></pre>

<p>When <code>package/__init__.py</code> is empty, the code works fine. However, when <code>package/__init__.py</code> contains <code>__all__ = []</code>, <code>print(sub_module)</code> will raise <code>NameError</code>.
What is <em>(This also works when <strong>all</strong> is defined.)</em> from the documentation means?</p>

<hr>

<p>The codes:</p>

<pre><code>package/
    __init__.py
    sub_module.py # empty file
main.py
</code></pre>

<p>In main.py:</p>

<pre><code>import package.sub_module
from package import *
print(sub_module)
</code></pre>

<p>When <code>package/__init__.py</code> is empty, executing <code>python3 main.py</code> gets 
    <code>&lt;module 'package.sub_module' from '/path/to/package/sub_module.py'</code></p>

<p>When <code>package/__init__.py</code> contains <code>__all__ = []</code>, executing <code>python3 main.py</code> gets</p>

<pre><code>Traceback (most recent call last):
File "main.py", line 3, in &lt;module&gt;
    print(sub_module)
NameError: name 'sub_module' is not defined
</code></pre>

<p><strong>Solution</strong>: you have <code>__all__</code> set to empty list i.e. <code>from package import *</code> basically imports nothing</p>

<p>set it to <code>__all__ = ['submodule']</code> in <code>__init__.py</code></p>

<hr>

<blockquote>
  <p>What exactly is <strong>__all__</strong> ?</p>
</blockquote>

<p>In simplest words <strong>all</strong> help customizing the <code>from package import *</code> i.e. with <strong>all</strong> we can set what will be imported and what not.</p>

<p><strong>From the <a href="https://docs.python.org/3/reference/simple_stmts.html#the-import-statement" rel="nofollow">docs</a></strong>:</p>

<blockquote>
  <p>The public names defined by a module are determined by checking the
  moduleâs namespace for a variable named <strong>all</strong>; if defined, it must
  be a sequence of strings which are names defined or imported by that
  module. The names given in <strong>all</strong> are all considered public and are
  required to exist. If <strong>all</strong> is not defined, the set of public names
  includes all names found in the moduleâs namespace which do not begin
  with an underscore character ('_'). <strong>all</strong> should contain the entire
  public API. It is intended to avoid accidentally exporting items that
  are not part of the API (such as library modules which were imported
  and used within the module).</p>
</blockquote>

<hr>

<p>One important thing to note here is - <strong>Imports without <code>*</code> are not affected by <code>__all__</code></strong> i.e. Members that are not mentioned in <code>__all__</code> are accessible from outside the module using direct import - <code>from &lt;module&gt; import &lt;member&gt;</code>.</p>

<p><strong>An Example</strong>: the following code in a <code>module.py</code> explicitly exports the symbols <code>foo</code> and <code>bar</code>:</p>

<pre><code>__all__ = ['foo', 'bar']

waz = 5
foo = 10
def bar(): return 'bar'
</code></pre>

<p>These symbols can then be imported like so:</p>

<pre><code>from foo import *

print foo
print bar

# now import `waz` will trigger an exception, 
# as it is not in the `__all__`, hence not a public member.
print waz
</code></pre>

python