Spotify-Playground / spotify-API

0 stars 0 forks source link

[Python] 현재 파일의 작업 디렉토리 이전하기 #5

Open hooniegit opened 11 months ago

hooniegit commented 11 months ago

Airflow DAG 실행 시 work directory는 DAG 스크립트가 위치한 디렉토리가 아닌, 환경 설정에 의해 지정된 디렉토리로 설정됩니다. 따라서 시스템 절대 경로를 사용하지 않는다면 DAG는 상대 경로를 인식하지 못하고 모듈 import 혹은 파일 실행에서 오류를 발생시키게 됩니다. 이를 예방하기 위해 '절대 경로를 자동으로 반환하는' 스크립트를 구상해 보겠습니다.

✏️ 코드

import os

os.chdir('/Users/kimdohoon/git/Spotify-Playground/spotify-API')

🧱 응용

  1. work directory를 변경하고, 변경한 work directory의 절대 경로 반환하기
    
    import os

os.chdir('/Users/kimdohoon/git/Spotify-Playground/spotify-API') now_dir = os.getcwd()


2. work directory를 변경하고, 변경한 work directory를 기준으로 모듈 import하기
``` python
import os, sys
os.chdir('/Users/kimdohoon/git/Spotify-Playground/spotify-API')
lib_dir = f"{os.getcwd()}/Airflow/sample_hooniegit/lib"
sys.path.append(lib_dir)
import spotipy_modules as lib_spotify