[Python] Running Python functions from other files

Discussão em 'Python' iniciado por Stack, Julho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I have created a number of .py files containing functions. The files would be organized in a Project_Repertory as follows:

    Project_Repertory

    • Load_Repertory
      • load_functions.py
      • main.py
    • Automate_Repertory
      • main.py

    In the Automate_Repertory I have a main.py file, which is to eventually be run using Apache Airflow (to which I am fully new), and I want to call the main() function of each of the other main.py files in the other folders.

    However, if I write from Load_Repertory.main import main, it fails with a ModuleNotFound error. I tried working from the "Project_Repertory" instead, and then when I do from Load_Repertory.main import main it fails with the same error but pointing to a line in the code saying from load_functions import function1.

    My Load_Repertory.main.py file looks like this:

    import re
    from load_functions import function1

    def main()
    print("This is main")
    function1()

    if __name__ == "__main__":
    main()


    I also tried importlib and runpy unsuccessfully. How do I import the main() from the Load_Repertory.main.py file and get it to run successfully if the terminal is open on the Automate_Repertory?

    Details: Python 3.13.3 on Windows 11.

    Continue reading...

Compartilhe esta Página