Correct way to re-export modules from __init__.py

Viewed 9592

I have a plugins package that contains several modules, each defining one class (each class is a plugin).

My package structure looks like this :

plugins
├ __init__.py
├ first_plugin.py
├ second_plugin.py
└ third_plugin.py

And a plugin file typically looks like this, only containing a class definition (and a few imports if necessary) :

# in first_plugin.py

class MyFirstPlugin:
    ...

I would like the end user to be able to import a plugin like so :

from plugins import FirstPlugin

instead of having to also type the module name (which is what is currently required to do) :

from plugins.first_plugin import FirstPlugin

Is there a way to achieve this by re-exporting the modules' classes directly in the __init__.py file without having to import everything module by module like so (which becomes cumbersome when there are lots of modules) :

# in __init__.py

from .first_plugin import FirstPlugin
from .second_plugin import SecondPlugin
from .third_plugin import ThirdPlugin
2 Answers

I do not think this is possible in Python. However you can import entire modules so you do not have to import each class individually.

For example

from first_plugin import *

Allowing you to do

from plugin import # Anything in first_plugin

Its kinda a pain but writing libraries is not easy (wait till you use CMake with C/C++, you have to specify every single file in your source tree :D)

I think you could elaborate on answers of this post: How to import all submodules?

For example with pkgutil.walk_packages(__path__) you'd have a list of modules. Then you could use dir on the loaded module and import the results (filtering elements starting with __

Related