I have a project in pure Python with a rudimentary plugin system: you write a module that defines a class with a specific interface and name, and the program imports the module and subsequently instantiates the class as needed.
Currently, the plugins all come from a specific folder (subdirectory of where the main .py file is located). I would like to be able to have them elsewhere on disk, and instruct the program to look for plugins in a specific place. Can I do this, for one-off dynamic imports, in a cleaner way than modifying sys.path? I don’t want to pollute this global.
Related: can I count on sys.path[0] being the path to the script, even if that differs from the current working directory (os.getcwd())?
EDIT: I forgot to mention – I want to be able to get plugins from several different folders, with the user specifying paths to plugin folders. Currently, each of these folders is set up as a package (with an __init__.py); I can trivially scrap this if it causes a problem.
This might seem weird, but you can modify a module’s
__path__variable and then import from it. Then you’re not messing with the global import space in sys.path.Edit: If the directories are loaded at run time, then you don’t need a plugins.py file to store them. You can create the module dynamically:
main.py:
After creating the dynamic module, you can load the plugins as before, using either
import_moduleor__import__:addons/plugins1/myplugin1.py:
addons/plugins2/myplugin2.py:
I’ve never used this, but it does work in both Python 2 & 3.