具有核心部分的命名空间包? [英] Namespace packages with a core part?

查看:49
本文介绍了具有核心部分的命名空间包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题跟进Python中制作命名空间包的方法如何在 Python 中创建命名空间包?.

注意 PEP 420分发文档,其中说明:

Note PEP 420, and the distribute docs, which state:

您不得在命名空间包的 __init__.py 中包含任何其他代码和数据.即使它可能在开发过程中看起来有效,或者当项目安装为 .egg 文件时,当项目使用系统"打包工具安装时它不会工作——在这种情况下,__init__.py 文件不会被安装,更不用说执行了.

You must NOT include any other code and data in a namespace package’s __init__.py. Even though it may appear to work during development, or when projects are installed as .egg files, it will not work when the projects are installed using "system" packaging tools – in such cases the __init__.py files will not be installed, let alone executed.

<小时>

这一切似乎让拥有独立分布的扩展子包的主库"包变得不可能.我想要的是能够:


This all seems to make it impossible to have a "main library" package with independently distributed extension sub-packages. What I want is to be able to:

  1. 定义一个核心库包,像这样使用:

  1. define a core library package, to be used like this:

import mylibrary

mylibrary.some_function()

  • 允许单独打包和分发的库扩展像这样使用:

  • allow library extensions, packaged and distributed separately, to be used like this:

    import mylibrary.myextension
    
    mylibrary.myextension.some_other_function()
    

  • 我本来希望能够使用命名空间包来做到这一点,但根据上面的问题和链接,情况似乎并非如此.这完全可以做到吗?

    I would've expected to be able to do this with namespace packages, but it seems not to be the case, based on the questions and links above. Can this be done at all?

    推荐答案

    对于 PEP 420 命名空间包,确实不可能在顶级 __init__.py 中包含代码.

    It is indeed not possible to have code in a top level __init__.py for a PEP 420 namespace package.

    如果我是你,我会:

    1. 创建 2 个包,一个称为 mylibrary(一个普通包),其中包含您的实际库代码,另一个称为 mylibrary_plugins,它是一个命名空间包.
    2. 或者,创建 mylibrary.lib(一个包含您的代码的普通包)和 mylibrary.plugins(一个命名空间包).

    我个人会使用选项 1.

    Personally I'd use option 1.

    PEP 420 的基本原理部分解释了为什么 __init__.py 不能包含任何代码.

    The rationale section of PEP 420 explains why __init__.py cannot contain any code.

    这篇关于具有核心部分的命名空间包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆