Skip to main content

dbt-python-imports

This is a dbt plugin to allow importing arbitrary Python modules from Jinja templates.

Usage

  1. Install dbt-python-imports via PyPI into the same Python environment as dbt itself.

  2. Call modules.import("<module_name>") to import the given module from a dbt Jinja string.

    Examples:

  3. Profit!

[!warning]

This allows arbitrary code execution. (That's kindof the point.)

Jinja's sandboxing is not foolproof (e.g. some adapters allow arbitrary reads/writes to disk), so you already shouldn't be running untrusted dbt code/packages. However the limited Jinja context available by default does currently make it harder to run arbitrary code.

Make sure you're not installing any packages (dbt or Python) whose source you haven't inspected.

Available dbt Jinja Contexts

Context Available?
model & hook SQL ✅
model properties.yml ✅
dbt_project.yml ⚠️ (partial: vars work, configs seem to not)
profiles.yml ❌

See unit tests for more details on context availability.

Example

# download the artifacts from your latest Databricks dbt job run

$ echo '
{#- call via `dbt build --state="$(dbt run-operation -q fetch_remote_state)" --defer ... #}
{%- macro fetch_remote_state(base_path=invocation_args_dict.project_dir ~ "/" ~ var("remote_state_base_path", "target/remote-state"), job_name="dbt build") %}
  {#- https://databricks-sdk-py.readthedocs.io/en/latest/workspace/jobs/jobs.html #}
  {%- set jobs_api = adapter.config.credentials.authenticate().api_client.jobs %}
  {%- set job = jobs_api.list(name=job_name, limit=1) | first %}
  {%- set job_run = jobs_api.list_runs(job_id=job["job_id"], limit=1, completed_only=true, expand_tasks=true) | first %}
  {%- set task_run = job_run["tasks"] | first %}
  {%- set task_run_output = jobs_api.get_run_output(run_id=task_run["run_id"]) %}
  {%- set download_url = task_run_output.dbt_output.artifacts_link %}

  {%- set state_path = base_path ~ "/" ~ task_run["run_id"] %}
  {%- set tar_path = state_path ~ "/dbt-artifacts.tar.gz" %}

  {%- set system = modules.import("dbt_common.clients.system") %}
  {%- do system.make_directory(state_path) %}
  {%- do system.download(download_url, tar_path) %}
  {%- do system.untar_package(tar_path, state_path) %}

  {#- print file path to stdout #}
  {%- do print(state_path ~ "/target") %}
{%- endmacro %}
' > macros/fetch_remote_state.sql

$ dbt run-operation -q fetch_remote_state
.../target/remote-state/55665502170299/target

Metadata

Release files for dbt-python-imports 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dbt-python-imports 0.1.1
File Size Uploaded
dbt_python_imports-0.1.1.tar.gz 2.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dbt-python-imports 0.1.1
File Interpreter ABI Platform
dbt_python_imports-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 5.8 kB

Release files / dbt_python_imports-0.1.1.tar.gz

Download URL dbt_python_imports-0.1.1.tar.gz
Size 2.8 kB
Tags Source
SHA-256 checksum
How to use checksums
206f5704eb5301e0e9a046a0cfdc5b000294e6ae173e21cd4b5dbedb46cf96e4
BLAKE2b-256 checksum
How to use checksums
2d2fb2cd88ecf519089d54bee8368f6a268c8fc37b2994aec7e6d80a443e8b39
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.10.8 {"installer":{"name":"uv","version":"0.10.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / dbt_python_imports-0.1.1-py3-none-any.whl

Download URL dbt_python_imports-0.1.1-py3-none-any.whl
Size 3.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eaffb19c8685e3049170a393770d20ab25f605b209746774072c397ffce4a68e
BLAKE2b-256 checksum
How to use checksums
c645c84fae5ac34607583f8787150cf81553d18de088d393cdf24682025110ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.10.8 {"installer":{"name":"uv","version":"0.10.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page