dbt-core
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
- breaking Explicit installation of both `dbt-core` and adapter plugins is now recommended. Prior to v1.8, installing an adapter (e.g., `dbt-snowflake`) implicitly installed `dbt-core`. While this implicit behavior is maintained for backward compatibility currently, it may be removed in future versions.
- gotcha Programmatic `dbtRunner` invocations in the same Python process do not support safe parallel execution. Running multiple dbt commands concurrently within a single process is discouraged due to potential interactions with global Python variables and data platform conflicts.
- deprecated Setting custom global config flags in `profiles.yml` has been deprecated. These flags should now be configured in `dbt_project.yml`.
- breaking Major versions (e.g., v1 to v2) of dbt Core may introduce breaking changes, including the removal of deprecated functionality. Minor versions (e.g., v1.8 to v1.9) are generally backward compatible for documented features.
- deprecated Beginning in v1.10, dbt Core will issue deprecation warnings for invalid dbt code, including custom inputs, duplicate YAML keys, and unexpected Jinja blocks, which will become invalid in future versions.
- gotcha dbt Python models (`.py` files) are executed remotely on the configured data platform (e.g., Snowflake, BigQuery, Databricks), not on the local machine where `dbt-core` is running. This means any Python code must be compatible with the platform's execution environment and its limitations (e.g., external API calls might not be supported).
Install
-
pip install dbt-core -
pip install dbt-core dbt-<adapter_name>
Imports
- dbtRunner
from dbt.cli.main import dbtRunner
- dbtRunnerResult
from dbt.cli.main import dbtRunnerResult
Quickstart
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}")