Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access environment variables in dbt Python model

  • https://docs.getdbt.com/docs/build/environment-variables
  • https://docs.getdbt.com/reference/dbt-jinja-functions/env_var

These documents only explain about how to access environment variables in dbt SQL model. How can I do it in dbt dython model?

like image 574
Yohei Onishi Avatar asked Sep 18 '26 21:09

Yohei Onishi


2 Answers

While you cannot expand jinja templates in python dbt models, i used to have a workaround.

Suppose you have variable defined in project config.

dbt_project.yml:

...
vars:
  my_var: my_value

then, you can use jinja in model config itself to pass this value.

my_model_scheme.yml

models:
  - name: model_my
    config:
      materialized: incremental
      tags: [ 'python' ]
      my_var: '{{ var("my_var") }}'

And finally get it in model.

model_my.py

def model(dbt, session):
    print(dbt.config.get("my_var"))
like image 118
Alex Joz Avatar answered Sep 20 '26 12:09

Alex Joz


I want to extend the existing answer. With environment variables, you do not need to set the configuration in the dbt_project.yml, even it is considered as best practice.

An alternative approach would be to use

.env:

my_var=ABCD

Here you can then directly access the variable using env_var

my_model_scheme.yml

models:
  - name: model_my
    config:
      materialized: incremental
      tags: [ 'python' ]
      my_var: '{{ env_var("my_var") }}'

And the rest is the same

model_my.py

def model(dbt, session):
    print(dbt.config.get("my_var"))
like image 33
Azngeek Avatar answered Sep 20 '26 12:09

Azngeek