You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Airflow中导入DAG时出现ImportError的技术问题求助

Fixing ImportError When Importing Airflow DAG and Operators

Let's break down what's causing this error and how to fix it quickly:

The Root Cause

Your script is named airflow.py — this is conflicting with the official Airflow library! When Python runs from airflow import DAG, it looks for modules in the current directory first. Instead of loading the actual Airflow library installed on your system, it's trying to import from your own airflow.py file, creating a circular import loop (which is why the traceback shows it's stuck on your local file).

Step-by-Step Fix

  1. Rename your DAG file
    Change the filename from airflow.py to something that doesn't clash with the library name, like daily_data_pipeline.py or my_s3_to_postgres_dag.py.

  2. Clean up cached files
    If there's an airflow.pyc file (Python's compiled bytecode) in your directory, delete it. This file might still reference the old naming and cause errors even after you rename the script.

  3. Update operator imports (for Airflow 2.x+)
    Looking at your code, you're importing S3KeySensor from airflow.operators — this path is outdated for Airflow 2.x. Update that import to:

    from airflow.providers.amazon.aws.sensors.s3_key import S3KeySensor
    

    (You'll need to make sure the apache-airflow-providers-amazon package is installed if you haven't already.)

Corrected Code Snippet

from airflow import DAG
from airflow.operators.bash import BashOperator
from airflow.providers.amazon.aws.sensors.s3_key import S3KeySensor
from datetime import datetime, timedelta, date
import psycopg2

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

# Add your DAG definition and task logic here...

Quick Additional Checks

  • Make sure your Airflow environment is properly configured (correct AIRFLOW_HOME setting, DAGs placed in the designated dags directory).
  • Confirm you're running the script in the virtual environment where Airflow is installed.

内容的提问来源于stack exchange,提问作者Vivek Mehendiratta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:19:39