简体   繁体   中英

Python namespace packages with Poetry

I am using Poetry to build some namespace packages. The directory's structure is the same for every package:

package_bbb
    pyproject.toml
    aaa
        bbb
            myservice.py
            __init__.py
        __init__.py

Using Poetry I can build a wheel, install it and use the package:

from aaa.bbb import myservice

My problem happens when I start to build a second package, that needs the first one as dependency.

The structure remains the same:

package_ccc
    pyproject.toml
    aaa
        ccc
            mydata.py
            __init__.py
        __init__.py

In Poetry I add the first one as dependency with:

poetry add package_bbb

The package will be installed (coming from an intern repository) in the generated virtualenv and added to pyproject.toml.

Also the generated wheel for the second package is perfect. The first package will be installed by pip as dependency.

The problem is during the development: If I try, for example inside the mydata.py , to import something from the first package:

from aaa.bbb import myservice

I have got a module not found. I think it is happening because I have 2 aaa namespaces: one in the virtualenv and one in the projects root directory . The second one is taking the precedence and there is no aaa.bbb.

Am I missing something in the package's structure, or there is a way to avoid this sort of "namespaces conflict"?

Thanks a lot for your help!

You need to remove the __init__.py from the aaa folder in both packages. Otherwise aaa will be detected as a normal package and not as a namespace package. See https://www.python.org/dev/peps/pep-0420/#specification for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM