0

I have a folder called Script_py, and containing a lot of .py files, for example tri_insertion.py and tri_rapide.py.

Each name.py contains just one function called also name. My aim is to :

  • import all the functions (and if I have to add an other .py file, it will be imported automatically),
  • execute one function with the command 'name(parameters)'.

I tried the solutions of How to load all modules in a folder? with a dynamic ___all___ in ___init___.py, and from Script_py import all, but a calling to a function is name.name(parameters) instead of name(parameters)

jowe_19
  • 101
  • 2

1 Answers1

0

Finally the following solution works fine. Fristly, I store in a list the complete list of modules :

import os, pkgutil
test = list(module for _, module, _ in 
            pkgutil.iter_modules([os.path.dirname('absolute_path')]))

Secondly, I import all the modules with a for loop.

for script in test:
    exec("from {module} import *".format(module=script))
jowe_19
  • 101
  • 2