Reputation: 79
main_folder/
__init__.py
sub_folder1/
__init__.py
run_all_functions.py
sub_folder2/
__init__.py
script1.py
script2.py
script3.py
script1.py
def something():
....
def something2():
....
script2.py
def example():
....
def example2():
....
script3.py
def example3():
....
Would like to check if there are ways that i can run all the functions in different scripts in folder2 and consolidate them in run_all_functions.py in folder1 dynamically. I might add more scripts with functions in folder 2 in the near future.
Upvotes: 2
Views: 1228
Reputation: 17342
Provided that you add a run_all()
entry-point function to each .py
file in a directory with your scripts, you could load all these file and call their respective run_all()
functions.
Here is a little demonstration, adjust DIR
.
import importlib.util
import pathlib
DIR = '/tmp/test'
for pyfile in pathlib.Path(DIR).glob('*.py'): # or perhaps 'script*.py'
spec = importlib.util.spec_from_file_location(f"{__name__}.imported_{pyfile.stem}" , pyfile)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
module.run_all()
Without a per module run_all()
, you must use some kind of introspection to find all functions to be run.
Upvotes: 3