pycomfy - A simple and robust Python client for ComfyUI
pycomfy is a Python library designed to make interacting with the ComfyUI API straightforward and developer-friendly. It handles complex tasks like workflow management and model downloading automatically, allowing you to focus on generating images.
Features
- Automatic Model Downloading: If a required model (checkpoint) is missing,
pycomfywill automatically download it from Hugging Face if you provide your local ComfyUI path. - High-Level Presets: Generate images with popular models like SD1.5, SD3, and FLUX using simple, one-line functions (e.g.,
api.text_to_image_sd15(...)). - Full Customization: A generic
api.text_to_image(...)function gives you full control over every parameter. - Expert Workflow Control: Load, inspect, and modify any custom ComfyUI workflow directly from your Python code.
- Zero-Fuss Experience: No need to restart the ComfyUI server after a model is downloaded. The library handles it, and your script continues seamlessly.
Installation
pip install pycomfy
Getting Started: The Simple Way
This is the easiest way to start generating images. The library takes care of everything.
- Provide your ComfyUI path: This is optional, but required to enable automatic model downloading.
- Call a preset function: Choose a model and provide your prompts.
from pycomfy import ComfyAPI, MissingModelError
# This is optional, but required for the automatic download feature.
# Replace with your actual path to the main ComfyUI folder.
COMFYUI_PATH = "C:/path/to/your/ComfyUI"
try:
# Initialize the API
api = ComfyAPI("127.0.0.1:8188", comfyui_path=COMFYUI_PATH)
# Generate an image using a high-level preset
# If the model is missing, it will be downloaded automatically.
print("Generating with the SD1.5 preset...")
images = api.text_to_image_sd15(
positive_prompt="a majestic lion on a throne, cinematic lighting, highly detailed",
negative_prompt="blurry, cartoon, bad art",
seed=42
)
if images:
images[0].save("lion_king.png")
print("Image 'lion_king.png' saved successfully!")
except MissingModelError as e:
print(f"Error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
Usage
1. Initialization
from pycomfy import ComfyAPI
# Basic initialization (automatic download disabled)
api = ComfyAPI("127.0.0.1:8188")
# Initialization with automatic download enabled
api = ComfyAPI("127.0.0.1:8188", comfyui_path="C:/path/to/your/ComfyUI")
2. Preset Functions
These are shortcuts for common text-to-image tasks. They use pre-configured settings that you can easily override.
# Stable Diffusion 1.5
images = api.text_to_image_sd15(positive_prompt="an astronaut riding a horse")
# Stable Diffusion 3
images = api.text_to_image_sd3(positive_prompt="an astronaut riding a horse")
# FLUX.1 Schnell
images = api.text_to_image_flux_schnell(positive_prompt="an astronaut riding a horse")
# FLUX.1 Dev
images = api.text_to_image_flux_dev(positive_prompt="an astronaut riding a horse")
# You can also override any parameter
images = api.text_to_image_flux_schnell(
positive_prompt="an astronaut riding a horse",
steps=10, # Default is 8 for this preset
seed=12345
)
3. Generic Text-to-Image Function
For full control when presets are not enough. Specify any parameter you need.
images = api.text_to_image(
positive_prompt="a cute cat programmer, 8k, highest quality",
negative_prompt="bad anatomy, watermark, blurry",
checkpoint_name="v1-5-pruned-emaonly-fp16.safetensors",
steps=25,
cfg=8.0,
sampler_name="dpmpp_2m_karras",
width=768,
height=512,
seed=9876
)
4. The Expert Way: Custom Workflows
For maximum flexibility, you can load a custom workflow JSON file (saved from ComfyUI with "Save (API Format)") and manipulate its nodes.
# 1. Load your custom workflow
workflow = api.load_workflow("my_special_workflow_api.json")
# 2. Find the nodes you want to change
sampler_nodes = workflow.get_nodes_by_class("KSampler")
checkpoint_nodes = workflow.get_nodes_by_class("CheckpointLoaderSimple")
# 3. Set new values for the nodes
if checkpoint_nodes:
workflow.set_node(checkpoint_nodes[0], {"ckpt_name": "sd_xl_base_1.0.safetensors"})
if sampler_nodes:
workflow.set_node(sampler_nodes[0], {"steps": 30, "cfg": 8.5})
# 4. Execute the workflow with dynamic prompts
images = workflow.execute(
positive_prompt="a beautiful landscape, matte painting, trending on artstation",
negative_prompt="ugly, jpeg artifacts",
seed=42
)
if images:
images[0].save("expert_mode_image.png")
Error Handling
The library uses custom exceptions to help you debug. The most common one is MissingModelError, which is raised if a model file is missing and could not be downloaded automatically (e.g., it's not in the known model database).
License
This project is licensed under the MIT License.
Metadata
Release files for pycomfy 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pycomfy-0.4.0.tar.gz | 13.0 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pycomfy-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.5 kB
Release files / pycomfy-0.4.0.tar.gz
| Download URL | pycomfy-0.4.0.tar.gz |
|---|---|
| Size | 13.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f3692295891452c9807d798f4773156772b120ba5421e051b3f793e8013ee597
|
|
BLAKE2b-256 checksum How to use checksums |
67eb713e5264ba7df33f973309f47e6d3fbe2bcbe4e01ed613241c7ed5e6b513
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 12, 2025.
Transparency logRelease files / pycomfy-0.4.0-py3-none-any.whl
| Download URL | pycomfy-0.4.0-py3-none-any.whl |
|---|---|
| Size | 12.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
dd5e6ba592c3439b36be2c51b2df16e2e706bbd65e7b4077a1982d8f049a0b54
|
|
BLAKE2b-256 checksum How to use checksums |
cc48548ae6c8f0852c396cf75741946e72585c2c576862575827fb6db77e0300
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 12, 2025.
Transparency logSecurity for pycomfy 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for pycomfy-0.4.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 12, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.