开发者

Organize a python library as plugins

I would like to create a library, say foolib, but to keep different subpackages separated, so to have barmodule, bazmodule, all under the same foolib main package. In other words, I want the client code to be able to do

开发者_JAVA百科
import foolib.barmodule
import foolib.bazmodule

but to distribute barmodule and bazmodule as two independent entities. Replace module with package as well... ba[rz]module can be a fukll fledged library with complex content.

The reason behind this choice is manifold:

  • I would like a user to install only barmodule if he needs so.
  • I would like to keep the modules relatively independent and lightweight.
  • but I would like to keep them under a common namespace.

jQuery has a similar structure with the plugins.

Is it feasible in python with the standard setuptools and install procedure ?


You may be looking for namespace packages. See also PEP 382.


Yes, simply create a foolib directory, add an __init__.py to it, and make each sub-module a .py file.

/foolib
    barmodule.py
    bazmodule.py

then you can import them like so:

from foolib import barmodule
barmodule.some_function()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜