[Python] How to hack python import to import a specific namespace package preceding others?

Discussão em 'Python' iniciado por Stack, Março 18, 2025.

  1. Stack

    Stack Membro Participativo

    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__.pys 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?


    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.

    Continue reading...

Compartilhe esta Página