I am writing a CLI-based web server control panel to which I am adding plugin support. I will need to import Python code files with arbitrary names from a /plugins
directory. How does one dynamically import code files from a particular directory?
Consider the common situation in which we have the files application.py
and foobar.py
. In application.py
we add the following code to run a method in foobar.py
:
import foobar
foobar.main()
How might I generalize this so that all files in the plugins
directory get imported?
Additionally, I will need to add a menu item "Run foobar plugin" so I will need a list of the plugin names to iterate over. How is this done in Python?
I am targeting Python 3 on Linux servers if that matters.