简体   繁体   English

Python 命名空间包与诗歌

[英]Python namespace packages with Poetry

I am using Poetry to build some namespace packages.我正在使用 Poetry 来构建一些命名空间包。 The directory's structure is the same for every package:每个 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:使用 Poetry 我可以构建一个轮子,安装它并使用 package:

from aaa.bbb import myservice

My problem happens when I start to build a second package, that needs the first one as dependency.我的问题发生在我开始构建第二个 package 时,它需要第一个作为依赖项。

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. package 将安装(来自实习生存储库)在生成的 virtualenv 中并添加到 pyproject.toml。

Also the generated wheel for the second package is perfect.第二个 package 的生成轮也很完美。 The first package will be installed by pip as dependency.第一个 package 将由 pip 作为依赖项安装。

The problem is during the development: If I try, for example inside the mydata.py , to import something from the first package:问题是在开发过程中:如果我尝试,例如在mydata.py中,从第一个 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 .我认为它正在发生,因为我有 2 个 aaa 命名空间:一个在 virtualenv 中,一个在项目根目录中。 The second one is taking the precedence and there is no aaa.bbb.第二个是优先的,没有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.您需要从两个包的aaa文件夹中删除__init__.py Otherwise aaa will be detected as a normal package and not as a namespace package.否则aaa将被检测为正常 package 而不是命名空间 package。 See https://www.python.org/dev/peps/pep-0420/#specification for more details.有关更多详细信息,请参阅https://www.python.org/dev/peps/pep-0420/#specification

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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