Skip to main content

dbt plugin to add `modules.import` method to Jinja context to import arbitrary Python modules

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dbt_python_imports-0.1.1.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dbt_python_imports-0.1.1-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file dbt_python_imports-0.1.1.tar.gz.

File metadata

  • Download URL: dbt_python_imports-0.1.1.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for dbt_python_imports-0.1.1.tar.gz
Algorithm Hash digest
SHA256 206f5704eb5301e0e9a046a0cfdc5b000294e6ae173e21cd4b5dbedb46cf96e4
MD5 939328b6975925f466df6b5468c5dff4
BLAKE2b-256 2d2fb2cd88ecf519089d54bee8368f6a268c8fc37b2994aec7e6d80a443e8b39

See more details on using hashes here.

File details

Details for the file dbt_python_imports-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: dbt_python_imports-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for dbt_python_imports-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eaffb19c8685e3049170a393770d20ab25f605b209746774072c397ffce4a68e
MD5 f74add740de585d27c8f73b9be703ddd
BLAKE2b-256 c645c84fae5ac34607583f8787150cf81553d18de088d393cdf24682025110ee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page