Reading a variable in Python
You've realized that using Variables within your Dags will allow a bit of control over the configuration in various environments, including your development and test Airflow servers. To keep from needing to update your Dag code for each, you decide to implement an Output_Path variable to store the location on your server where the various files are kept.
The dag and task objects have been imported for you, along with datetime.
This exercise is part of the course
Introduction to Apache Airflow in Python
Exercise instructions
- Import the library necessary to work with Variables.
- Read the
Output_Pathvariable from Airflow. - Provide a fallback value of
/homeifOutput_Pathis not defined.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import the proper library to read Variables
from airflow.____ import ____
@dag(dag_id='process_sales', start_date=datetime(2026, 4, 15))
def process_sales():
@task()
def parse_file():
# Get the output file location, otherwise default to "/home"
output_path = ____.____("Output_Path", ____="/home")
# Logging only currently - processing to be added later
print(f"File parsed and saved to {output_path}/sales_report.pdf")
parse_file()
process_sales()