Skip to content

pythonExtension

Execute Python scripts in Trigger.dev tasks.

Install

bash
npm add @trigger.dev/python

Setup

ts
import { pythonExtension } from "@trigger.dev/python/extension";

export default defineConfig({
  project: "<project ref>",
  build: {
    extensions: [pythonExtension()],
  },
});

Running Python Inline

ts
import { python } from "@trigger.dev/python";

export const myScript = task({
  id: "my-python-script",
  run: async () => {
    const result = await python.runInline(`print("Hello, world!")`);
    return result.stdout;
  },
});

Running Python Script Files

Add scripts to the extension config so they're copied to the build directory:

ts
pythonExtension({
  scripts: ["./python/**/*.py"],
})

Then run them:

ts
const result = await python.runScript("./python/my_script.py", ["hello", "world"]);

The same path works in both dev and production.

Requirements File

Install Python packages during build (production only):

ts
pythonExtension({
  requirementsFile: "./requirements.txt",
})

Virtual Environments (dev only)

ts
pythonExtension({
  devPythonBinaryPath: ".venv/bin/python",
})

Streaming Output

ts
const result = python.stream.runScript("./python/my_script.py", ["hello", "world"]);
for await (const chunk of result) {
  console.log(chunk);
}

Environment Variables

process.env vars are automatically injected and accessible in Python via os.environ.

Pass extra vars:

ts
python.runScript("./python/script.py", [], { env: { MY_VAR: "value" } });

Built from official Trigger.dev documentation