> For the complete documentation index, see [llms.txt](https://docs.stacksync.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.stacksync.com/agentic-workflows/features/code-execution/python-code-execution.md).

# Python Code Execution

Run custom Python scripts directly inside your workflow. Use it for data transformation, complex logic, reshaping payloads, or anything that can't be handled by a standard module.

### 1. Add the Module

Add the **Python Code** module to your workflow as an action node.

<figure><img src="/files/DQzH7pbaPefV5zj8afg6" alt=""><figcaption></figcaption></figure>

### 2. Pass Data In Context

In the module configuration panel, add context variables under the **Context** section. Each entry takes a **Name** and a **Value,** use the variable picker to map values from upstream nodes.

Each context entry becomes a key on `WORKFLOW_CONTEXT` inside your script:

```python
def main(WORKFLOW_CONTEXT):
    amount = WORKFLOW_CONTEXT["amount"]
```

<figure><img src="/files/mTv3fGDxjl9bw43NLWWU" alt=""><figcaption></figcaption></figure>

### 3. Write Your Script

Your script must define a `main` function. Stacksync automatically injects `WORKFLOW_CONTEXT` as the argument containing all your context values.

```python
def main(WORKFLOW_CONTEXT):
    amount = WORKFLOW_CONTEXT["amount"]
    return {
        "amount": amount,
    }
```

### 4. Return Data

Whatever `main` returns becomes the node's output, available to downstream nodes via standard node referencing.

<figure><img src="/files/PymN5aBMUVsKS6vLV5CD" alt=""><figcaption></figcaption></figure>

Reference in downstream nodes:

```jinja
{{ module_id.amount }}
```

> **Debugging:** `print()` output is captured and surfaced under `metadata.stdout` in the execution logs. Use it for debugging — it does not pass data downstream.

### Available Libraries

| Library                      | Use case                             |
| ---------------------------- | ------------------------------------ |
| `pandas`                     | Data manipulation and transformation |
| `numpy`                      | Numerical operations                 |
| `phonenumbers`               | Phone number parsing and formatting  |
| `json`                       | JSON parsing                         |
| `os`                         | OS-level utilities                   |
| Python 3.10 standard library | All built-in modules                 |

### Limits

| Limit            | Value                |
| ---------------- | -------------------- |
| Timeout          | 300s (5 min)         |
| Memory           | 700 MB               |
| Filesystem       | Read-only, no writes |
| Runtime packages | Pre-installed only   |

> **Watch out:** Large pandas DataFrames can hit the 700 MB memory limit. Process data in chunks where possible.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.stacksync.com/agentic-workflows/features/code-execution/python-code-execution.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
