Reputation: 14997
I'm trying to run pytest on some functions I've written in Airflow. I'm using Python 3.8, btw.
The folder structure is
airflow
-- dags
-- includes
-- common
-- functions.py
-- tests
-- test_functions.py
I tried from includes.common.functions import functionA, functionB
, but kept getting the error ModuleNotFoundError: No module named 'functions'
.
If I try from dags.includes.common.functions import functionA, functionB
, I get ModuleNotFoundError: No module named 'dags'
.
I've tried python -m pytest tests
in the airflow
folder, same import error.
I'm not very familiar with importing modules, and would appreciate any help in how to solve this error.
Upvotes: 1
Views: 546
Reputation: 14997
I added the following to my script, and was able to run pytest
without the import errors.
import sys
sys.path.insert(0, '../dags/')
from includes.common.functions import functionA, functionB
I was also able to run it without any __init__.py
in any of the folders.
This post helped me.
Upvotes: 1
Reputation: 834
You can use .
to go one directory up to search for files. Use this import statement inside test_functions.py
:
from ..dags.includes.common.functions import functionA, functionB
Also, every folder needs an empty __init.py__
file. Create them for the folders airflow, dags, includes, common and tests.
Upvotes: 1