I have a folder containing various Python files, each including some functions called tasks. I want to do two things:
Instead of writing
from module.file1 import function1, function2
from module.file2 import function3
I would prefer to do:
from module import function1, function2, function3
In addition to that, I need a list of all functions names (as str) which available in this directory (excluding built-ins).
So far I tried writing a __init__.py with the following content:
from os.path import dirname, basename, isfile, join
import glob
modules = glob.glob(join(dirname(__file__), "*.py"))
functions = []
for m in modules:
if isfile(m) and not m.endswith('__init__.py'):
functions = [getattr(m, f) for f in dir(m) if callable(getattr(m, f))]
print(functions)
However, functions now contains all built-in functions but not the one was was looking for. In addition, how do I "return" the functions for import and how can I get the list of function names (string only) for further use?
mis a string, not a module.getattr(m, f)somehow helps, nowfunctionscontains a lot of stuff. It does include all builtin functions though and not the function I was hoping for.str.dir(m)should list the attributes of the modules identified by the path, right?