Search code examples
python-3.xapache-sparkmodel-view-controllerairflow

how to resolve ModuleNotFoundError python error


exampleProject
--> models   
----> database
-------> __init__.py
-------> example1.py
-------> example1.py
--> services
----> __init__.py
----> service1.py
----> service1.py

i have created this table structure for my project and the problem is when i am importing example1.py file in service1.py i am getting ModuleNotFoundError and same error getting when importing service2.py file in example2.py

i have tried sys.path.append method. but still getting same error, files imported in init.py file.

i want to run this project in production, i had read somewhere its not good practice to use sys.path.append method in production.

this is normal structure of folder but if i want to increase folders and file and import file from various folder how can i do that? can somebody help me in this, how to resolve this problem.

thank you.


Solution

  • first of all, the dags folder gets added to the sys path. Hence anything inside dags folder can be imported in Airflow env.

    I will take an example that you have given. So let's consider the below folder structure resides inside dags folder eg /home/airflow/dags

    exampleProject
    --> models   
    ----> database
    -------> __init__.py
    -------> example1.py
    --> services
    ----> __init__.py
    ----> service1.py
    

    Lets assume that exampleProject/models/database/example1.py looks something like this

    def example1_func():
        print("In example1_func")
    

    Now, let's import the above function inside the services/service1.py, so below is how we will import the example1_func

    from exampleProject.models.database.example1 import example1_func
    
    def service1_func():
        example1_func()
        print('inside service1_func')
    

    Now we can call the service1_func in our tasks by importing in similar method

    from exampleProject.services.service1 import service1_func
    
    # creating dag
    ...
    
    
    # calling the function in a task
    service1_func()
    ...