izak izak - 10 months ago 54
Python Question

Cannot import from package in same namespace tree until pkg_resources has been imported

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.

I have two packages, for argument's sake

, 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

except ImportError:
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

Now for some strange reason, I get this:

>>> import ns.protobuf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named protobuf
>>> import pkg_resources
>>> import ns.protobuf

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.

Answer Source

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