dbt-core

1.11.7 · active · verified Sat Mar 28

dbt-core is the open-source foundation of dbt (data build tool), empowering data analysts and engineers to transform data in their warehouses using SQL, following software engineering best practices. It's a powerful CLI tool for building, testing, documenting, and deploying data models. `dbt-core` is currently at version 1.11.7 and adheres to semantic versioning, with minor versions being backward compatible and frequent patch releases for bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically invoke dbt CLI commands using the `dbtRunner` class. It shows how to run all models in an existing dbt project and process the results. A `dbt_project.yml` and `profiles.yml` must be set up for this to execute successfully. The `dbtRunner` class was introduced in dbt Core v1.5 as the official Python entry point.

import os
from dbt.cli.main import dbtRunner, dbtRunnerResult

# NOTE: For this to run, you must have a dbt project initialized
# (e.g., with 'dbt init <project_name>') and a profiles.yml configured.
# Set the DBT_PROFILES_DIR environment variable or ensure 'dbt_project.yml'
# is in the current working directory or a parent directory.
# A simple dbt_project.yml might look like:
# name: my_dbt_project
# version: '1.0.0'
# config-version: 2
# profile: my_profile

# Initialize the dbt runner
dbt = dbtRunner()

# Example: Run all models in your dbt project
# You might need to set the project directory explicitly if not in CWD
project_directory = os.environ.get('DBT_PROJECT_DIR', os.getcwd())
cli_args = ["run", "--project-dir", project_directory]

print(f"Invoking dbt run with arguments: {cli_args}")
res: dbtRunnerResult = dbt.invoke(cli_args)

if res.success:
    print("dbt run completed successfully.")
    if res.result:
        for r in res.result:
            print(f"  Model: {r.node.name}, Status: {r.status}")
else:
    print("dbt run failed.")
    if res.exception:
        print(f"Exception: {res.exception}")
    elif res.result:
        # Some commands might return partial results even on failure
        print("Partial results or errors:")
        for r in res.result:
            print(f"  Model: {r.node.name}, Status: {r.status}, Message: {r.message}")

view raw JSON →