Python Forum
Plugin extendable program. How?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plugin extendable program. How?
#1
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?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#2
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__)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020