I am having an issue with python namespaced packages and am wondering what is a good solution.
My project structure is similar to the following
project_name/ext/app_ext
That is the project I'm working on. It has dependencies that are also in the same namespace.
project_name/ext/base_ext
project_name/ext/other_dependency
I am working on the "app_ext" namespaced package in a django application. However, I am running into issues receiving a no module named app_ext
.
I think it has to do with how the dependency namespaced packages are installed (I'm installing with PIP). The dependency namespaced packages aren't installed with the __init__.py
files as is documented in the distribute / setuptools documentation, but all truly contain the following declarations in their installation packages.
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
Now to the real question. Is there a trick to working with a namespaced package when there is already some related namespaced packages installed?
One solution is to modify the path of the namespaced package to add your local path. (if you are working in Django, you could add a snippet like the following to your settings.py).
from os import path
import sys
PACKAGE_NAMESPACE = ["project_name", "ext"]
VIRTUAL_PACKAGE = '.'.join(PACKAGE_NAMESPACE)
__import__(VIRTUAL_PACKAGE)
local_package = path.abspath(path.join(*PACKAGE_NAMESPACE))
sys.modules[VIRTUAL_PACKAGE].__dict__["__path__"].insert(0, local_package)
This approach is also used in the tiddlyweb project for the tiddlywebplugins namespace.