2
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.

2
  • If the files define a module you could use inspect to load all the methods declared for each module. Then you only have to repeat the process through all the files. This might be useful: stackoverflow.com/questions/139180/… Commented Aug 31, 2021 at 7:52
  • You could use exec, ie. open the .py files one by one and run exec on the text, like: with open(filename, 'r') as file: exec(file.read()) Commented Aug 31, 2021 at 7:54

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.