pythonsnowflake-cloud-data-platformdbt

How to access environment variables in dbt Python model


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


Solution

  • 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"))