Skip to content

feat(build): Add support for Python scripts via pythonExtension #1686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
2a4aa8c
add PythonExtension
zvictor Feb 9, 2025
987ab35
remove potential shell injection risk
zvictor Feb 9, 2025
8b97cec
Filter out blank lines or comment lines
zvictor Feb 9, 2025
84699e5
fix spelling
zvictor Feb 9, 2025
d00d147
add pythonExtension's `runInline`
zvictor Feb 9, 2025
500d82b
changes to requirements don’t invalidate the entire install layer
zvictor Feb 9, 2025
f0b30d6
copy script files on-demand
zvictor Feb 9, 2025
b63554e
improve PythonExtension types and logging
zvictor Feb 9, 2025
8ad7009
add changeset
zvictor Feb 9, 2025
c3cbd61
fix broken imports
zvictor Feb 10, 2025
be2cefe
Improve security of inline script execution
zvictor Feb 10, 2025
bb59bf8
Add file existence check for requirementsFile
zvictor Feb 10, 2025
65d84d0
update lock file
zvictor Feb 10, 2025
7c96930
Enhance error handling with detailed error information
zvictor Feb 10, 2025
c599809
Add portable type annotation
zvictor Feb 10, 2025
015f3aa
fix error TS18046: 'e' is of type 'unknown'
zvictor Feb 10, 2025
4302077
export the python extension
zvictor Feb 10, 2025
8ef78ee
add `pythonExtension` to the catalog
zvictor Feb 10, 2025
ca51709
fix `Cannot find module '@trigger.dev/build/extensions/core' (TS2307)
zvictor Feb 11, 2025
ddc706a
replace execa by tinyexec
zvictor Feb 11, 2025
311aed3
Merge branch 'main' into main
zvictor Feb 11, 2025
0e2d2e5
Merge branch 'main' into main
matt-aitken Feb 11, 2025
cf95fca
Update pnpm-lock.yaml
matt-aitken Feb 11, 2025
914d323
Merge branch 'main' into main
zvictor Feb 14, 2025
c246120
add custom traces instead of logging
zvictor Feb 16, 2025
b0f08c4
Merge branch 'triggerdotdev:main' into main
zvictor Feb 25, 2025
4776637
The cleanup in the finally block does not fail silently anymore
zvictor Feb 25, 2025
be76d69
move python runtime/extension to independent package
zvictor Feb 25, 2025
ec5c390
fix build package readme
zvictor Feb 25, 2025
cccf3e0
update lock file
zvictor Feb 25, 2025
a433061
add documentation to python's package
zvictor Feb 25, 2025
751ac55
add missing dependency
zvictor Feb 25, 2025
7eddd20
Update little-trains-begin.md
ericallam Feb 26, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/little-trains-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@trigger.dev/python": patch
---

Introduced a new Python extension to enhance the build process. It now allows users to execute Python scripts with improved support and error handling.
4 changes: 2 additions & 2 deletions packages/build/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Official TypeScript SDK for Trigger.dev
# Official Build Package of Trigger.dev

View the full documentation for the [here](https://trigger.dev/docs)
View the full documentation [here](https://trigger.dev/docs)
1 change: 1 addition & 0 deletions packages/python/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @trigger.dev/python
21 changes: 21 additions & 0 deletions packages/python/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 Trigger.dev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
102 changes: 102 additions & 0 deletions packages/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Python Extension for Trigger.dev

The Python extension enhances Trigger.dev's build process by enabling limited support for executing Python scripts within your tasks.

## Overview

This extension introduces the <code>pythonExtension</code> build extension, which offers several key capabilities:

- **Install Python Dependencies (Except in Dev):** Automatically installs Python and specified dependencies using <code>pip</code>.
- **Requirements File Support:** You can specify dependencies in a <code>requirements.txt</code> file.
- **Inline Requirements:** Define dependencies directly within your <code>trigger.config.ts</code> file using the <code>requirements</code> option.
- **Virtual Environment:** Creates a virtual environment (<code>/opt/venv</code>) inside containers to isolate Python dependencies.
- **Helper Functions:** Provides a variety of functions for executing Python code:
- <code>run</code>: Executes Python commands with proper environment setup.
- <code>runInline</code>: Executes inline Python code directly from Node.
- <code>runScript</code>: Executes standalone <code>.py</code> script files.
- **Custom Python Path:** In development, you can configure <code>pythonBinaryPath</code> to point to a custom Python installation.

## Usage

1. Add the extension to your <code>trigger.config.ts</code> file:

```typescript
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
pythonBinaryPath: path.join(rootDir, `.venv/bin/python`), // Optional: Custom Python binary path
scripts: ["my_script.py"], // List of Python scripts to include
}),
],
},
});
```

2. (Optional) Create a <code>requirements.txt</code> file in your project root with the necessary Python dependencies.

3. Execute Python scripts within your tasks using one of the provided functions:

### Running a Python Script

```typescript
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;
},
});
```

### Running Inline Python Code

```typescript
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

pandas.to_datetime("${+new Date() / 1000}")
`);
return result.stdout;
},
});
```

### Running Lower-Level Commands

```typescript
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
},
});
```

## Limitations

- This is a **partial implementation** and does not provide full Python support as an execution runtime for tasks.
- Only basic Python script execution is supported; scripts are not automatically copied to staging/production containers.
- Manual intervention may be required for installing and configuring binary dependencies in development environments.

## Additional Information

For more detailed documentation, visit the official docs at [Trigger.dev Documentation](https://trigger.dev/docs).
93 changes: 93 additions & 0 deletions packages/python/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"name": "@trigger.dev/python",
"version": "3.3.16",
"description": "Python runtime and build extension for Trigger.dev",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "https://github.com/triggerdotdev/trigger.dev",
"directory": "packages/python"
},
"type": "module",
"files": [
"dist"
],
"tshy": {
"selfLink": false,
"main": true,
"module": true,
"project": "./tsconfig.src.json",
"exports": {
"./package.json": "./package.json",
".": "./src/index.ts",
"./extension": "./src/extension.ts"
},
"sourceDialects": [
"@triggerdotdev/source"
]
},
"typesVersions": {
"*": {
"extension": [
"dist/commonjs/extension.d.ts"
]
}
},
"scripts": {
"clean": "rimraf dist",
"build": "tshy && pnpm run update-version",
"dev": "tshy --watch",
"typecheck": "tsc --noEmit -p tsconfig.src.json",
"update-version": "tsx ../../scripts/updateVersion.ts",
"check-exports": "attw --pack ."
},
"dependencies": {
"@trigger.dev/build": "workspace:3.3.16",
"@trigger.dev/core": "workspace:3.3.16",
"@trigger.dev/sdk": "workspace:3.3.16",
"tinyexec": "^0.3.2"
},
"devDependencies": {
"@types/node": "20.14.14",
"rimraf": "6.0.1",
"tshy": "^3.0.2",
"typescript": "^5.5.4",
"tsx": "4.17.0",
"esbuild": "^0.23.0",
"@arethetypeswrong/cli": "^0.15.4"
},
"engines": {
"node": ">=18.20.0"
},
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"@triggerdotdev/source": "./src/index.ts",
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
},
"./extension": {
"import": {
"@triggerdotdev/source": "./src/extension.ts",
"types": "./dist/esm/extension.d.ts",
"default": "./dist/esm/extension.js"
},
"require": {
"types": "./dist/commonjs/extension.d.ts",
"default": "./dist/commonjs/extension.js"
}
}
},
"main": "./dist/commonjs/index.js",
"types": "./dist/commonjs/index.d.ts",
"module": "./dist/esm/index.js"
}
119 changes: 119 additions & 0 deletions packages/python/src/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import fs from "node:fs";
import assert from "node:assert";
import { additionalFiles } from "@trigger.dev/build/extensions/core";
import { BuildManifest } from "@trigger.dev/core/v3";
import { BuildContext, BuildExtension } from "@trigger.dev/core/v3/build";

export type PythonOptions = {
requirements?: string[];
requirementsFile?: string;
/**
* [Dev-only] The path to the python binary.
*
* @remarks
* This option is typically used during local development or in specific testing environments
* where a particular Python installation needs to be targeted. It should point to the full path of the python executable.
*
* Example: `/usr/bin/python3` or `C:\\Python39\\python.exe`
*/
pythonBinaryPath?: string;
/**
* An array of glob patterns that specify which Python scripts are allowed to be executed.
*
* @remarks
* These scripts will be copied to the container during the build process.
*/
scripts?: string[];
};

const splitAndCleanComments = (str: string) =>
str
.split("\n")
.map((line) => line.trim())
.filter((line) => line && !line.startsWith("#"));

export function pythonExtension(options: PythonOptions = {}): BuildExtension {
return new PythonExtension(options);
}

class PythonExtension implements BuildExtension {
public readonly name = "PythonExtension";

constructor(private options: PythonOptions = {}) {
assert(
!(this.options.requirements && this.options.requirementsFile),
"Cannot specify both requirements and requirementsFile"
);

if (this.options.requirementsFile) {
assert(
fs.existsSync(this.options.requirementsFile),
`Requirements file not found: ${this.options.requirementsFile}`
);
this.options.requirements = splitAndCleanComments(
fs.readFileSync(this.options.requirementsFile, "utf-8")
);
}
}

async onBuildComplete(context: BuildContext, manifest: BuildManifest) {
await additionalFiles({
files: this.options.scripts ?? [],
}).onBuildComplete!(context, manifest);

if (context.target === "dev") {
if (this.options.pythonBinaryPath) {
process.env.PYTHON_BIN_PATH = this.options.pythonBinaryPath;
}

return;
}

context.logger.debug(`Adding ${this.name} to the build`);

context.addLayer({
id: "python-installation",
image: {
instructions: splitAndCleanComments(`
# Install Python
RUN apt-get update && apt-get install -y --no-install-recommends \
python3 python3-pip python3-venv && \
apt-get clean && rm -rf /var/lib/apt/lists/*

# Set up Python environment
RUN python3 -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
`),
},
deploy: {
env: {
PYTHON_BIN_PATH: `/opt/venv/bin/python`,
},
override: true,
},
});

context.addLayer({
id: "python-dependencies",
build: {
env: {
REQUIREMENTS_CONTENT: this.options.requirements?.join("\n") || "",
},
},
image: {
instructions: splitAndCleanComments(`
ARG REQUIREMENTS_CONTENT
RUN echo "$REQUIREMENTS_CONTENT" > requirements.txt

# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
`),
},
deploy: {
override: true,
},
});
}
}

export default pythonExtension;
Loading