Worth mentioning about the getattr and hasattr which can help you get an attribute (e.g. the plugin class) or check of the module has the attribute respectively. Those can help you provide feedback and tell users that something expected is not found. Both can also be used for checking for the execute method. I like how you presented the import_module and it is very hand when you need to import things at run time.
Sorry for the stupid question, but why don't you need an __init__.py file to import plugins.one? In the documentation it says you need an __init__.py file inside the folder.
Since Python 3.3+ you no longer need a empty __init__.py file just to make a folder into a package. If you want a cleaner API you still have to use the __init__py though, since otherwise still have to use package.module.class when you perhaps want it to be only package.class for smaller packages.
Awesome video Dan! The production is really beautiful! May I make a request? Recently, since python3.4, the pathlib module has been adopted through out the standard library for path handling. I think the community would really benefit from some content that explains how/when to use path lib, in comparison to the os.path.
I'd like to have my python file call the __main__ from another file. However, the other __main__ expects command-line arguments... how can I call it and wait for until it returns/exits before continuing my file's execution?
first off, great video, just what im looking for im trying to build a python script load that open up a window dialog to allow user to choose which file to load so i have a question,i notice you said that u want to load relative to current folder so is something like this possible? module=importlib.import_module(modname,Path(r'C: a\Dropbox\My python files\guide'))
Good question-I'm not a big fan of eval because it makes debugging more difficult. It would certainly be possible to hack together plugin loading using eval (load the code from a file, run it through eval) but I'd probably lean towards importing things as a module.
Thank you for this nice tutorial. My problem is how can I get a permanent global handle for the module on every iteration? In your example, I need to use the module outright inside the loop after it gets assigned the handle 'plugin_module' in L7 of app.py. Because on the next iteration of the loop, 'plugin_module' will not be anymore one, but it will be two. I've tried assigning to globals()[plugin_module] on each loop but it seems to be not working either. Thanks!
Well you could have a list above the loop and when in that loop append to module to it. Then elsewhere for another module action loop the list and do it then
You should watch it again. At 9:22, you simply replace PLUGIN_NAME with a directory scan of the plugins directory. He's defined it only for the example.