|
1 |
| -# Official Python extension for Trigger.dev |
| 1 | +# Python Extension for Trigger.dev |
2 | 2 |
|
3 |
| -View the full documentation [here](https://trigger.dev/docs) |
| 3 | +The Python extension enhances Trigger.dev's build process by enabling limited support for executing Python scripts within your tasks. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This extension introduces the <code>pythonExtension</code> build extension, which offers several key capabilities: |
| 8 | + |
| 9 | +- **Install Python Dependencies (Except in Dev):** Automatically installs Python and specified dependencies using <code>pip</code>. |
| 10 | +- **Requirements File Support:** You can specify dependencies in a <code>requirements.txt</code> file. |
| 11 | +- **Inline Requirements:** Define dependencies directly within your <code>trigger.config.ts</code> file using the <code>requirements</code> option. |
| 12 | +- **Virtual Environment:** Creates a virtual environment (<code>/opt/venv</code>) inside containers to isolate Python dependencies. |
| 13 | +- **Helper Functions:** Provides a variety of functions for executing Python code: |
| 14 | + - <code>run</code>: Executes Python commands with proper environment setup. |
| 15 | + - <code>runInline</code>: Executes inline Python code directly from Node. |
| 16 | + - <code>runScript</code>: Executes standalone <code>.py</code> script files. |
| 17 | +- **Custom Python Path:** In development, you can configure <code>pythonBinaryPath</code> to point to a custom Python installation. |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +1. Add the extension to your <code>trigger.config.ts</code> file: |
| 22 | + |
| 23 | +```typescript |
| 24 | +import { defineConfig } from "@trigger.dev/sdk/v3"; |
| 25 | +import pythonExtension from "@trigger.dev/python/extension"; |
| 26 | + |
| 27 | +export default defineConfig({ |
| 28 | + project: "<project ref>", |
| 29 | + build: { |
| 30 | + extensions: [ |
| 31 | + pythonExtension({ |
| 32 | + requirementsFile: "./requirements.txt", // Optional: Path to your requirements file |
| 33 | + pythonBinaryPath: path.join(rootDir, `.venv/bin/python`), // Optional: Custom Python binary path |
| 34 | + scripts: ["my_script.py"], // List of Python scripts to include |
| 35 | + }), |
| 36 | + ], |
| 37 | + }, |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +2. (Optional) Create a <code>requirements.txt</code> file in your project root with the necessary Python dependencies. |
| 42 | + |
| 43 | +3. Execute Python scripts within your tasks using one of the provided functions: |
| 44 | + |
| 45 | +### Running a Python Script |
| 46 | + |
| 47 | +```typescript |
| 48 | +import { task } from "@trigger.dev/sdk/v3"; |
| 49 | +import python from "@trigger.dev/python"; |
| 50 | + |
| 51 | +export const myScript = task({ |
| 52 | + id: "my-python-script", |
| 53 | + run: async () => { |
| 54 | + const result = await python.runScript("my_script.py", ["hello", "world"]); |
| 55 | + return result.stdout; |
| 56 | + }, |
| 57 | +}); |
| 58 | +``` |
| 59 | + |
| 60 | +### Running Inline Python Code |
| 61 | + |
| 62 | +```typescript |
| 63 | +import { task } from "@trigger.dev/sdk/v3"; |
| 64 | +import python from "@trigger.dev/python"; |
| 65 | + |
| 66 | +export const myTask = task({ |
| 67 | + id: "to_datetime-task", |
| 68 | + run: async () => { |
| 69 | + const result = await python.runInline(` |
| 70 | +import pandas as pd |
| 71 | +
|
| 72 | +pandas.to_datetime("${+new Date() / 1000}") |
| 73 | +`); |
| 74 | + return result.stdout; |
| 75 | + }, |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +### Running Lower-Level Commands |
| 80 | + |
| 81 | +```typescript |
| 82 | +import { task } from "@trigger.dev/sdk/v3"; |
| 83 | +import python from "@trigger.dev/python"; |
| 84 | + |
| 85 | +export const pythonVersionTask = task({ |
| 86 | + id: "python-version-task", |
| 87 | + run: async () => { |
| 88 | + const result = await python.run(["--version"]); |
| 89 | + return result.stdout; // Expected output: Python 3.12.8 |
| 90 | + }, |
| 91 | +}); |
| 92 | +``` |
| 93 | + |
| 94 | +## Limitations |
| 95 | + |
| 96 | +- This is a **partial implementation** and does not provide full Python support as an execution runtime for tasks. |
| 97 | +- Only basic Python script execution is supported; scripts are not automatically copied to staging/production containers. |
| 98 | +- Manual intervention may be required for installing and configuring binary dependencies in development environments. |
| 99 | + |
| 100 | +## Additional Information |
| 101 | + |
| 102 | +For more detailed documentation, visit the official docs at [Trigger.dev Documentation](https://trigger.dev/docs). |
0 commit comments