Cannot import from package in same namespace tree until pkg_resources has been imported
<p>I have a strange problem that I somehow cannot reproduce separately, yet it shows up in production code, and of course the production code cannot be shared publically.</p>

<p>I have two packages, for argument's sake <code>ns.server</code> and <code>ns.protobuf</code>, where the latter implements protobuf specific extensions for the project. Both packages properly declare the namespace packages in setup.py, and both have the boilerplate pkg_resources stuff in <code>__init__.py</code>:</p>

<pre><code>try:
    __import__('pkg_resources').declare_namespace(__name__)
except ImportError:
    from pkgutil import extend_path
    __path__ = extend_path(__path__, __name__)
</code></pre>

<p>Now for some strange reason, I get this:</p>

<pre><code>&gt;&gt;&gt; import server.protobuf
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
ImportError: No module named protobuf
&gt;&gt;&gt; import pkg_resources
&gt;&gt;&gt; import server.protobuf
&gt;&gt;&gt; 
</code></pre>

<p>So it appears that my namespaces are all screwy until I import pkg_resources and then it is fixed. It's not too bad, the workaround is then simply to import pkg_resources first. I would just like to understand what is going on.</p>

<p>Ugh, second question I self-answer in as many days. I had a stale egg-info directory lying around in <code>lib/python2.7/site-packages</code>, from a previous install where I accidentally neglected to pass -e (development mode) to pip. Completely clearing everything and reinstalling fixed it.</p>

python python-2.7 pkg-resources