Execute Python with the Code block
Learn how to use Python code in a workflow.
Use Code blocks to write custom Python code that can transform data and perform complex logic. You can also use popular Python libraries to further extend the functionality of workflows.
To use Python code blocks on self-hosted instances, you must have the code-executor container configured.
{{ }} curly braces around embedded expressions are not required in Python.
Add a Python Code block
To use Python in a workflow, add a Code block to the canvas and select Python.
Write and execute Python code
The Python code editor has much of the same features as the JavaScript editor, such as autocomplete and syntax highlighting. For example, you can transform an array of records in a similar manner to map() with JavaScript:
data = query1.data
return [{
"fullName": customer['name'],
"emailAddress": customer['email']
} for customer in data]

Python limitations
JavaScript is the primary method for manipulating and transforming data in Retool. To maintain interoperability with other blocks, Python does not support:
- Function block calls or triggering queries.
- Usage within Loop and Filter blocks.
- Data output in types other than serialized JSON.
- User-imported Python libraries.
Configure Python version
| Python runtime version selector and 3.14 Availability | |||
|---|---|---|---|
| Cloud | Generally Available | ||
| Self-hosted Edge Expected in an upcoming edge release. | |||
| Self-hosted Stable Expected in an upcoming stable release. | |||
To update the Python version used by a workflow:
- Open the Languages & Libraries tab.
- Click ... next to Python.
- Select Update version.
- Choose a version and click Apply.
The available versions are 3.10 (default) and 3.14. Python 3.14 does not include built-in libraries. If you are upgrading to Python 3.14, Retool automatically identifies any built-in libraries your workflow uses and adds them as custom libraries in requirements.txt.
Python 3.14 is mostly backwards-compatible but may include changes that break existing Python code. Retool recommends publishing a release before upgrading so you can revert, if necessary.
When you click Apply to upgrade to a new version of Python, what happens depends on whether your workflow has a published release. If you have unpublished changes, a dialog appears with the following options:
- Publish snapshot & upgrade: Publishes your current changes as a new release, then upgrades to Python 3.14. Creates a restore point you can revert to if the upgrade breaks your workflow.
- Upgrade without publishing: Upgrades to Python 3.14 without publishing a snapshot first. You won't have an automatic restore point if the upgrade breaks your workflow.
- Cancel upgrade: Returns you to the upgrade flow without making changes.
Revert to a previous release
If your workflow does not function correctly after upgrading, you can restore a previous release from the Releases panel:
- Open the workflow and click Releases in the toolbar.
- Locate the release you want to restore.
- Click ... next to the release and select Revert to this version.
Use Python libraries
Retool supports many popular libraries, and you can import custom libraries from PyPI.
Built-in libraries
Workflows includes built-in support for many popular libraries. This enables you to extend the functionality of workflows beyond data transformation.
To use a built-in Python library:
- Open the Libraries tab, and click +.
- Select Add Python library.
- Search for the libraries you want to include, and toggle the checkbox to make it available.
- Click Add selected libraries.
You can browse through all built-in libraries below. Some libraries are only available on self-hosted instances.
Add custom libraries
Retool automatically tries to use NsJail to sandbox the creation of execution environments for custom libraries. NsJail requires privileged container access. While NsJail is not required to use custom libraries, it is strongly recommended.
If your deployment framework does not support privileged container access, you can host the code-executor service in a separate cluster that can run containers in privileged mode. You then configure the CODE_EXECUTOR_INGRESS_DOMAIN environment variable to communicate with the code-executor service over http or https.
Both cloud and self-hosted instances can import public packages from PyPI. Self-hosted instances can also import private packages from PyPI.
Add a public PyPI repository
As with typical Python development, you provide a list of libraries to use in a requirements.txt file. Retool makes this available through the Workflow editor. To populate it, navigate to the Libraries tab, click +, then select Modify requirements.txt.
bson==0.5.10
numpy==1.24.1
pandas==1.5.2
Add a private PyPI respository
Private PyPI repositories are only available on self-hosted instances that have a configured code-executor service.
Configuring a private PyPI repository requires setting of two environment variables in the code executor deployment: PYPI_REPOSITORIES and TRUSTED_HOSTS.
PYPI_REPOSITORIESis a comma-separated list of domains, and each entry is the same format as index-url flag in pip. The default value is https://pypi.org/simple. Some configurations are below:TRUSTED_HOSTSis a comma-separated list of domains, and each entry is a domain name, similar to the trusted-host flag in pip. This allows for authentication through pip and may not be required in most circumstances.
- Public and private repositories
- Private repositories only
PYPI_REPOSITORIES=https://pypi.org/simple,https://repository.example.com/simple
TRUSTED_HOSTS=pypi.org,repository.example.com
PYPI_REPOSITORIES=https://repository.example.com/simple
TRUSTED_HOSTS=repository.example.com
Once configured, you can install packages through the Modify requirements.txt option in the Workflows editor using the same steps listed in the Add a public PyPI repository section.
my_internal_library==0.1.0
Configure block settings
Refer to the Code block reference for information about the block's settings.