Build a workflow in ComfyUI, then walk away with runnable Python.
ComfyUI-to-Python-Extension turns visual workflows into executable scripts so you can move from node graphs to automation, experiments, and repeatable generation without rebuilding everything by hand.
This project supports:
- exporting from the ComfyUI UI with
Save As Script - converting saved API-format workflows with the CLI
Choose the setup that matches how you want to use the project. This project supports Python 3.12 and newer.
For ComfyUI to recognize this project as an extension, the repo must be discoverable through ComfyUI's custom_nodes search paths.
Use one of these setups:
- Clone directly into
ComfyUI/custom_nodes
cd /path/to/ComfyUI/custom_nodes
git clone https://github.com/pydn/ComfyUI-to-Python-Extension.git
cd ComfyUI-to-Python-Extension
uv sync- Keep the repo elsewhere, then either:
- symlink it into
ComfyUI/custom_nodes - add its parent directory to ComfyUI's
custom_nodessearch paths viaextra_model_paths.yaml
Example symlink setup:
git clone https://github.com/pydn/ComfyUI-to-Python-Extension.git
cd /path/to/ComfyUI/custom_nodes
ln -s /path/to/ComfyUI-to-Python-Extension ComfyUI-to-Python-Extension
cd /path/to/ComfyUI-to-Python-Extension
uv syncAfter installation, restart ComfyUI.
You can keep the repo anywhere for CLI usage and generated-script execution.
git clone https://github.com/pydn/ComfyUI-to-Python-Extension.git
cd ComfyUI-to-Python-Extension
uv sync
export COMFYUI_PATH=/path/to/ComfyUICOMFYUI_PATH helps the exporter and generated scripts find the ComfyUI codebase. It does not, by itself, register this repo as a ComfyUI extension for the Web UI.
COMFYUI_PATH is checked first. If it is not set, the exporter falls back to searching parent directories for a folder named ComfyUI.
In current ComfyUI builds, Save As Script is typically available under:
File -> Save As Script
The command downloads a generated .py file.
The current UI export uses the default filename workflow_api.py so it works in ComfyUI Desktop without relying on prompt().
Notes:
- menu placement can differ between frontend versions
- the Web UI export uses a fixed default filename rather than asking for one interactively
- In ComfyUI, enable dev mode options if needed.
- Save the workflow in API format:
File -> Export (API). - Run the exporter:
uv run python -m comfyui_to_pythonOptions:
uv run python -m comfyui_to_python \
--input_file workflow_api.json \
--output_file workflow_api.py \
--queue_size 10The legacy wrapper still works if you prefer it:
uv run python comfyui_to_python.pyFlags:
--input_file: input workflow JSON, defaultworkflow_api.json--output_file: output Python file, defaultworkflow_api.py--queue_size: default execution count in the generated script, default10
Generated scripts depend on a working ComfyUI runtime.
If the repo is not inside ComfyUI, set:
export COMFYUI_PATH=/path/to/ComfyUIThe generated script is a workflow export. It does not automatically turn workflow inputs into command-line arguments.
Scripts exported directly from File -> Save As Script in the ComfyUI UI already include the frontend workflow metadata needed for drag-and-drop reimport. Images saved by those scripts can be dropped back into ComfyUI and reopen with the original workflow metadata.
Generated scripts reuse ComfyUI's runtime argument parser during bootstrap, so common ComfyUI memory flags such as --highvram, --normalvram, --lowvram, --novram, --cpu, and --disable-smart-memory can be passed directly to the exported .py file.
Lifecycle notes:
- exported scripts are single-shot workflow runners, not long-lived ComfyUI prompt servers
- they do not implement Web UI prompt/result caching across repeated service calls
- exported
main()now performs best-effort ComfyUI model/cache cleanup in afinallyblock - set
COMFYUI_TOPYTHON_UNLOAD_MODELS=1or callmain(unload_models=True)if an embedded or repeated-call host should aggressively unload models after each run instead of preserving them for reuse
- unsupported Python version:
use Python 3.12 or newer, then rerun
uv sync Save As Scriptnot visible: check your current ComfyUI menu/frontend version and look underFileSave As Scriptnot visible after restart: make sure this repo is discoverable by ComfyUI throughcustom_nodesby cloning it intoComfyUI/custom_nodes, symlinking it there, or adding an externalcustom_nodespath inextra_model_paths.yaml- save uses the default filename:
rename
workflow_api.pyafter download if you want a different local filename - ComfyUI cannot be found:
set
COMFYUI_PATH - models or paths are missing at runtime:
verify the target ComfyUI install and its
extra_model_paths.yaml

