Can't import namespace package "c4d" but any other name for it works
Question: I've built myself a collection of Python libraries which I collect under the
`nr` namespace. Now for a Python plugin in an application called C4D, I want
to use the `nr.c4d` module, but it can't be imported. Strangely, if I rename
it to anything else (eg. `nr.c4dlib`), it works!

    
    
    + nr.c4d\
      + nr\
        * __init__.py  # < pkgutil.extend_path() in here
        + c4d\
          * __init__.py
          * gui.py
          * ...
    

This is the code that is in each namespace `__init__.py`

    
    
    # This is a namespace package.
    try:
      import pkg_resources
      pkg_resources.declare_namespace(__name__)
    except ImportError:
      import pkgutil
      __path__ = pkgutil.extend_path(__path__, __name__)
    

The `nr.__path__` list seems fine, too.

    
    
    ['C:\\maxon\\Cinema 4D R16 Dev\\plugins\\procedural\\python\\nr',
     'C:\\maxon\\Cinema 4D R16 Dev\\plugins\\cloudui_frontend_r16\\devel\\nr.strex\\nr',
     'C:\\maxon\\Cinema 4D R16 Dev\\plugins\\cloudui_frontend_r16\\devel\\nr.concurrency\\nr',
     'C:\\maxon\\Cinema 4D R16 Dev\\plugins\\cloudui_frontend_r16\\devel\\nr.cli\\nr',
     'C:\\maxon\\Cinema 4D R16 Dev\\plugins\\cloudui_frontend_r16\\devel\\nr.c4d\\nr']
    

But I get

    
    
    Traceback (most recent call last):
      File "'cloudui_frontend.pyp'", line 176, in <module>
    ImportError: No module named c4d
    

It might be relevant that there is a built-in module called `c4d` in the
environment. What is the source of this error? How to fix it?

* * *

_Update_ The problem seems to come from another namespace module called
`nr.procedural`. It does `import c4d` at the top of the file. If that line is
commented out, importing `nr.c4d` works! Also, instead of commenting out the
line, I can add `from __future__ import absolute_import`.


Answer: Relative imports caused a `None`-entry to be created for `nr.c4d` in
`sys.modules` before `nr.__path__` was extended a second time. This is the
issue I opened on my `localimport` repository:

> **Scenario** : `nr.procedural` does `import c4d`. If `nr.c4d` does not
> exist, the module will be added as a None-entry to `sys.modules`. If then
> `_localimport` is used to import `nr.c4d`, the module will not be found as
> the module cache says that the module does not exist (by the None-entry).
>
> **Solution** : `_localimport` must delete None-entries for namespace
> packages on `__enter__()`.

The
[fix](https://github.com/NiklasRosenstein/localimport/commit/842d2aee2b37782441c53143d46864a597e080d4)
was relatively easy.

I will need to add `from __future__ import absolute_import` to all `nr.` sub-
packages that `import c4d` though.

