问题
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?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/14402112/namespace-packages-with-a-core-part