packages/python/README.md
The Python extension enhances Trigger.dev's build process by enabling limited support for executing Python scripts within your tasks.
This extension introduces the <code>pythonExtension</code> build extension, which offers several key capabilities:
import { defineConfig } from "@trigger.dev/sdk/v3";
import { pythonExtension } from "@trigger.dev/python/extension";
export default defineConfig({
project: "<project ref>",
build: {
extensions: [
pythonExtension({
requirementsFile: "./requirements.txt", // Optional: Path to your requirements file
devPythonBinaryPath: ".venv/bin/python", // Optional: Custom Python binary path
scripts: ["src/python/**/*.py"], // Glob pattern for Python scripts
}),
],
},
});
pandas==1.3.3
numpy==1.21.2
import { defineConfig } from "@trigger.dev/sdk/v3";
import { pythonExtension } from "@trigger.dev/python/extension";
export default defineConfig({
project: "<project ref>",
build: {
extensions: [
pythonExtension({
requirementsFile: "./requirements.txt",
}),
],
},
});
import { task } from "@trigger.dev/sdk/v3";
import { python } from "@trigger.dev/python";
export const myScript = task({
id: "my-python-script",
run: async () => {
const result = await python.runScript("my_script.py", ["hello", "world"]);
return result.stdout;
},
});
export const myStreamingScript = task({
id: "my-streaming-python-script",
run: async () => {
// You can also stream the output of the script
const result = python.stream.runScript("my_script.py", ["hello", "world"]);
// result is an async iterable/readable stream
for await (const chunk of streamingResult) {
logger.debug("convert-url-to-markdown", {
url: payload.url,
chunk,
});
}
},
});
import { task } from "@trigger.dev/sdk/v3";
import { python } from "@trigger.dev/python";
export const myTask = task({
id: "to_datetime-task",
run: async () => {
const result = await python.runInline(`
import pandas as pd
pd.to_datetime("${+new Date() / 1000}")
`);
return result.stdout;
},
});
import { task } from "@trigger.dev/sdk/v3";
import { python } from "@trigger.dev/python";
export const pythonVersionTask = task({
id: "python-version-task",
run: async () => {
const result = await python.run(["--version"]);
return result.stdout; // Expected output: Python 3.12.8
},
});
For more detailed documentation, visit the official docs at Trigger.dev Documentation.