According to PEP420, namespace packages have the lowest precedence. For example, import xxx
will not create a namespace package xxx
if there are any xxx/__init__.py
s or xxx.{py,pyc,so,pyd}
s in any path in sys.path
.
I have a folder /path/xxx
that contains some code and I have been using it as a namespace package by using sys.path.insert(0,'/path')
and then import xxx
. But now I somehow get another regular package also named xxx
that shadows the namespace package I want.
My question is, how can I import /path/xxx
no matter what other packages may exist? Or better, as namespace package is just a list of paths (which can be retrieved from its __path__
attribute), how can I manually create a namespace package from a given list of paths?
P.S.
I tried searching for ways to do it through importlib
but find no results.
I cannot make changes to /path/xxx
, like adding an __init__.py
to it.
After some digging, I finally got it to work.
import importlib
import sys
# 1. manually create a namespace package specification
spec = importlib.machinery.ModuleSpec("xxx", None)
spec.submodule_search_locations = ["/path/xxx"]
# 2. import module from the spec
xxx = importlib.util.module_from_spec(spec)
# 3. add it to global cache
sys.modules["xxx"] = xxx