Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

    import mylibrary
    
    mylibrary.some_function()
    
  2. 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?

like image 393
jbenet Avatar asked Jan 18 '13 15:01

jbenet


1 Answers

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:

  1. 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.
  2. 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.

like image 123
Eric V. Smith Avatar answered Oct 04 '22 20:10

Eric V. Smith