Namespace packages with a core part?

This question follows up The way to make namespace packages in Python and How do I create a namespace package in Python?.

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

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:

  • 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?


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

    If I were you, I'd either:

  • create 2 packages, one called mylibrary (a normal package) which contains your actual library code, and the other called mylibrary_plugins which is a namespace package.
  • or, create mylibrary.lib, which is a normal package and contains your code, and mylibrary.plugins, which is a namespace package.
  • Personally I'd use option 1.

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


    strictly speaking, you can have variables under mylibrary , you just won't be able to define them there. You can, for instance:

    # mylibrary/core.py
    import mylibrary
    def some_function():
        pass
    
    mylibrary.some_function = some_function
    

    and your users can use it like:

    import mylibrary.core
    mylibrary.some_function()
    

    That is to say, mylibrary.core monkey patches mylibrary so that, other than the import, it looks as though somefunction is defined in mylibrary rather than a sub-package.

    链接地址: http://www.djcxy.com/p/68660.html

    上一篇: url()函数不能在共享主机上工作

    下一篇: 具有核心部分的命名空间包?