-1

In essence, I'm trying to make an extension system, where each plugin hooks into the important functions via the respective function in the file. I need a way to run this function and get the return value, by just looping through the "plugins" directory.

Any way I could do this?

MiseroMCS
  • 39
  • 2
  • You may find [this answer](http://stackoverflow.com/a/26623508/4014959) helpful, although it is written for Python 2. – PM 2Ring Aug 23 '16 at 14:16

1 Answers1

1

you can import files dinamicaly using __import__

so you just need to iterate the folder looking for py files (not pyc) and import them

for root, dirs, files in os.walk(src_path):
    for f in files:
        if f.endswith('.py'):
            m = __import__(f)

m will now be the instanc of the module , so if you have a function called my_func under it, you can do:

m.my_func()

or if you have the name of the function as string:

getattr(m,'my_func')()

DorElias
  • 2,243
  • 15
  • 18
  • I actually have 1 issue with this, when I try to run the function, it says "module 'plugins' has no attritube 'function'". Any idea why this is happening? – MiseroMCS Aug 23 '16 at 14:41
  • well it seems python finds your module but not function (did you actually called your function `function`?) try defining other things in your module and see if it can access them , or add some of your code, so i can help you – DorElias Aug 23 '16 at 15:50