I am not a programmer. I code for myself. Sometimes for my friends too. For now.
How to write a program allowing plugins? I was thinking that it should just check a directory and what is there loads it - import. Is this the approach or something else?
I'm not a professional programmer either, but I once found a nice way to do it. Suppose my package is named
foobar
. I created a configuration file in python
~/.foobar.d/plugin_path.py
. This configuration file contains a list of system paths to directories that contain plugins. Then I create a submodule
foobar.plugin
. In this submodule, the configuration file is read and the list of directories is appended to the list
globals()['__path__']
.
The idea is to use a feature of the import mechanism,
https://docs.python.org/3/tutorial/modul...irectories
Now I can write a module
/ham/spam/eggs/coolplugin.py
and write
from foobar.plugin import coolplugin
The only install step is to add
/ham/spam/eggs
in the configuration file (the plugin path).
Remark 1:
foobar.plugin
needs to be a package for this to work. The code that reads the configuration file goes in
__init__.py
Remark 2: With this method, one can also traverse the available plugins by using
pkgutil.iter_modules(foobar.plugin.__path__)