Skip to content

Instantly share code, notes, and snippets.

@voberoi
Created June 18, 2018 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voberoi/8b1f028ab99a51bcda9dba9643552222 to your computer and use it in GitHub Desktop.
Save voberoi/8b1f028ab99a51bcda9dba9643552222 to your computer and use it in GitHub Desktop.
Cloud Composer Dependency Issue (June 2018)
import airflow
from airflow.operators.bash_operator import BashOperator
from airflow.models import DAG
from foo_dep import print_something
args = {"owner": "airflow", "start_date": airflow.utils.dates.days_ago(2)}
dag = DAG(dag_id="foo", default_args=args, schedule_interval="0 0 * * *")
a_bash_cmd = BashOperator(task_id="a_bash_cmd", bash_command="echo 1", dag=dag)
if __name__ == "__main__":
dag.cli()
def print_something():
print "Something."
@voberoi
Copy link
Author

voberoi commented Jun 18, 2018

Upload both of the above to your Cloud Composer bucket under dags/.

The DAG will be broken because it can't load the dependency:

screen shot 2018-06-18 at 1 48 46 pm

This works locally, however.

@bikramsisodia
Copy link

bikramsisodia commented Jun 18, 2018

Create a subfolder and have all dependent modules in that. Bundle everything as zip file and upload to dag folder. https://airflow.apache.org/concepts.html#packaged-dags

@voberoi
Copy link
Author

voberoi commented Jun 18, 2018

Thanks!

For posterity, create packages instead of modules at the top-level, like so:

foo_dag.py
foo_dep/__init__.py
foo_dep/foo_dep.py

Make sure foo_dag imports the dependency correctly. The Gist above won't work, but from foo_dep.foo_dep import print_something will.

Now, zip that folder structure and upload it to your /dags folder on GCS. Your Airflow environment will pick it up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment