요약
- airflow는 자동으로 dags 폴더와 plugins 폴더를 경로에 추가함
- local과 airflow 경로 불일치 오류를 해결하기 위해
.env파일 생성
1. Python 모듈 경로 이해
import sys
from pprint import pprint
pprint(sys.path) # 현재 python 경로 확인Airflow 장점: 자동으로 dags와 plugins 폴더를 sys.path에 추가함
2. plugins 폴더 활용
# plugins/common/common_func.py 에 함수 작성 후
from common.common_func import get_sftp오류 원인
| 환경 | 작성 방법 | 이유 |
|---|---|---|
| VSCode (개발환경) | from plugins.common.common_func | 최상위 폴더인 airflow 폴더가 path로 잡힘 |
| Airflow (가상환경) | from common.common_func | plugins 폴더가 이미 path로 잡혀있음 |
해결 방안
.env 파일에 PYTHONPATH 추가:
WORKSPACE_FOLDER=/Users/haejun/airflow
PYTHONPATH=${WORKSPACE_FOLDER}/plugins3. 외부 함수 실행 예시
from airflow import DAG
import pendulum
from airflow.operators.python import PythonOperator
from common.common_func import get_sftp
with DAG(
dag_id="dags_python_import_func",
schedule="23 8 * * *",
start_date=pendulum.datetime(2024, 8, 22, tz="Asia/Seoul"),
catchup=False
) as dag:
task_get_sftp = PythonOperator(
task_id='task_get_sftp',
python_callable=get_sftp
)
task_get_sftp참고사이트