Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError : cannot import DAG airflow

I have simple code, I am trying to import DAG from airflow

from airflow import DAG
from airflow.operators import BashOperator,S3KeySensor
from datetime import datetime, timedelta
import psycopg2
from datetime import date, timedelta

yesterday = date.today() - timedelta(1)
yesterdayDate = yesterday.strftime('%Y-%m-%d')

But, I am getting Import Error

Traceback (most recent call last):
  File "airflow.py", line 9, in <module>
    from airflow import DAG
  File "/home/ubuntu/airflow/dags/airflow.py", line 9, in <module>
    from airflow import DAG
ImportError: cannot import name DAG

apache-airflow version installed 1.9.0

I don't know where I am going wrong

like image 471
Vivek Mehendiratta Avatar asked May 02 '18 10:05

Vivek Mehendiratta


1 Answers

This is similar to Package import failure in Python 3.5

When you name your Python script airflow.py, the statement from airflow import DAG ends up trying to import DAG from the script itself, not the airflow package.

The lesson is to never name your *.py files the same as built-in modules or 3rd party packages you have installed.

like image 195
Erik Cederstrand Avatar answered Sep 18 '22 19:09

Erik Cederstrand