Files
James Ritchie fade22015f Docs – v4 GA updates (#2298)
* Adds new features table to top of v4 upgrade guide

* Adds wait idempotency to wait-until, wait-for, and wait-for-token pages

* Adds new priority docs page and updates the v4 upgrade guide

* Adds new task lifecycle hooks

* Removes the message about requiring tasks to be exported

* Adds new global lifecycle hooks section

* Moves sections from upgrade guide into the table

* Adds hidden task page

* Improves the global lifecycle hooks section

* Updates middleware and locals section

* Adds new useWaitToken page to the react hooks section

* Adds a new ai.tool section

* Moves Docker (legacy) page into self-hosting section

* Removes known issues from v4 upgrade guide

* Replace “toolTask” with “ai.tool” in the Streams page example

* Renames guide to “Migrating from v3” and adds redirect

* Remove references to v4

* Removes changelog from migration guide

* The installation guide now references `@latest update`

* Changes all references from `/sdk/v3` to `/sdk`

* Updates @v4-beta to @latest

* Fixed broken link

* Fixes broken link

* Adds an upgrade to v4 using AI section

* Fixes 2 broken links

* Adds an entry for targetting preview branches

* Updates the run statuses

* Adds boolean helpers section to the runs and realtime pages

* Updates the concurrency page

* Updates the test page to include the new options

* Adds SDK and curl options for the preview branch targeting

* Updates new bulk actions page

* Remove the releasing concurrency section

* Got rid of some more @v4-beta mentions

* Improved rate limit docs

* Improved migrating docs

* Removed commented sections of the docs

* useWaitToken hook

* Fixed the description

* Fix for missing test image

---------

Co-authored-by: Matt Aitken <matt@mattaitken.com>
Co-authored-by: Dan <8297864+D-K-P@users.noreply.github.com>
2025-08-18 12:34:55 +01:00

183 lines
5.5 KiB
Plaintext

---
title: "Python"
sidebarTitle: "pythonExtension"
description: "Use the python build extension to add support for executing Python scripts in your project"
---
If you need to execute Python scripts in your Trigger.dev project, you can use the `pythonExtension` build extension via the `@trigger.dev/python` package.
First, you'll need to install the `@trigger.dev/python` package:
```bash
npm add @trigger.dev/python
```
Then, you can use the `pythonExtension` build extension in your `trigger.config.ts` file:
```ts
import { defineConfig } from "@trigger.dev/sdk";
import { pythonExtension } from "@trigger.dev/python/extension";
export default defineConfig({
project: "<project ref>",
build: {
extensions: [pythonExtension()],
},
});
```
This will take care of adding python to the build image and setting up the necessary environment variables to execute Python scripts. You can then use our `python` utilities in the `@trigger.dev/python` package to execute Python scripts in your tasks. For example, running a Python script inline in a task:
```ts
import { task } from "@trigger.dev/sdk";
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;
},
});
```
## Adding python scripts
You can automatically add python scripts to your project using the `scripts` option in the `pythonExtension` function. This will copy the specified scripts to the build directory during the deploy process. For example:
```ts
import { defineConfig } from "@trigger.dev/sdk";
import { pythonExtension } from "@trigger.dev/python/extension";
export default defineConfig({
project: "<project ref>",
build: {
extensions: [
pythonExtension({
scripts: ["./python/**/*.py"],
}),
],
},
});
```
This will copy all Python files in the `python` directory to the build directory during the deploy process. You can then execute these scripts using the `python.runScript` function:
```ts
import { task } from "@trigger.dev/sdk";
import { python } from "@trigger.dev/python";
export const myScript = task({
id: "my-python-script",
run: async () => {
const result = await python.runScript("./python/my_script.py", ["hello", "world"]);
return result.stdout;
},
});
```
<Note>
The pythonExtension will also take care of moving the scripts to the correct location during `dev`
mode, so you can use the same exact path in development as you do in production.
</Note>
## Using requirements files
If you have a `requirements.txt` file in your project, you can use the `requirementsFile` option in the `pythonExtension` function to install the required packages during the build process. For example:
```ts
import { defineConfig } from "@trigger.dev/sdk";
import { pythonExtension } from "@trigger.dev/python/extension";
export default defineConfig({
project: "<project ref>",
build: {
extensions: [
pythonExtension({
requirementsFile: "./requirements.txt",
}),
],
},
});
```
This will install the packages specified in the `requirements.txt` file during the build process. You can then use these packages in your Python scripts.
<Note>
The `requirementsFile` option is only available in production mode. In development mode, you can
install the required packages manually using the `pip` command.
</Note>
## Virtual environments
If you are using a virtual environment in your project, you can use the `devPythonBinaryPath` option in the `pythonExtension` function to specify the path to the Python binary in the virtual environment. For example:
```ts
import { defineConfig } from "@trigger.dev/sdk";
import { pythonExtension } from "@trigger.dev/python/extension";
export default defineConfig({
project: "<project ref>",
build: {
extensions: [
pythonExtension({
devPythonBinaryPath: ".venv/bin/python",
}),
],
},
});
```
This has no effect in production mode, but in development mode, it will use the specified Python binary to execute Python scripts.
## Streaming output
All of the `python` functions have a streaming version that allows you to stream the output of the Python script as it runs. For example:
```ts
import { task } from "@trigger.dev/sdk";
import { python } from "@trigger.dev/python";
export const myStreamingScript = task({
id: "my-streaming-python-script",
run: async () => {
// You don't need to await the result
const result = python.stream.runScript("./python/my_script.py", ["hello", "world"]);
// result is an async iterable/readable stream
for await (const chunk of streamingResult) {
console.log(chunk);
}
},
});
```
## Environment variables
We automatically inject the environment variables in the `process.env` object when running Python scripts. You can access these environment variables in your Python scripts using the `os.environ` dictionary. For example:
```python
import os
print(os.environ["MY_ENV_VAR"])
```
You can also pass additional environment variables to the Python script using the `env` option in the `python.runScript` function. For example:
```ts
import { task } from "@trigger.dev/sdk";
import { python } from "@trigger.dev/python";
export const myScript = task({
id: "my-python-script",
run: async () => {
const result = await python.runScript("./python/my_script.py", ["hello", "world"], {
env: {
MY_ENV_VAR: "my value",
},
});
return result.stdout;
},
});
```