Skip to content
AnswerWell geo Documentation

Research · 8 min read · Answerwell

Python Workers Entrypoints, Pywrangler, Frameworks, and Stateful Bindings

How Cloudflare Python Workers run on Pyodide, use WorkerEntrypoint handlers, Pywrangler, WSGI/ASGI apps, Durable Objects, and Workflows.

This page summarises Cloudflare’s documentation on Python Workers: running Python at the edge with the same bindings and runtime APIs as JavaScript Workers, using Pyodide inside a V8 isolate, class-based entrypoints, and the Pywrangler CLI for packages and deployment.

Python Workers: what runs your code at the edge

Section titled “Python Workers: what runs your code at the edge”

Python Workers execute your code through Pyodide, which is CPython compiled to WebAssembly. When you deploy a Python Worker, the runtime interprets your code in Pyodide within a V8 isolate—the same execution model as other Workers, without a separate precompile step on your machine.

Workers written in Python are executed by Pyodide ↗︎. Pyodide is a CPython ↗︎ (the reference implementation of Python — commonly referred to as just “Python”) compiled to WebAssembly.

— Cloudflare, How Python Workers Work

Cloudflare Workers advertises a first-class Python path: installable packages (including FastAPI, LangChain, and Pydantic), a foreign function interface (FFI) to JavaScript APIs and bindings, and platform services (KV, D1, Durable Objects, R2, Queues, Workflows, Workers AI, and more) via bindings.

python_workers compatibility flag: how to turn Python on

Section titled “python_workers compatibility flag: how to turn Python on”

You must enable Python explicitly in Wrangler. The python_workers compatibility flag turns on first-class Python support:

This flag enables first class support for Python. Python Workers implement the majority of Python’s standard library, support all bindings, environment variable, and secrets, and integration with JavaScript objects and functions via a foreign function interface.

— Cloudflare, Compatibility flags

Set compatibility_flags to include python_workers in wrangler.jsonc or wrangler.toml, and set compatibility_date to a date that matches the Python/Pyodide version you need (see Pyodide and Python versions below).

WorkerEntrypoint and the Default class: where HTTP handlers live

Section titled “WorkerEntrypoint and the Default class: where HTTP handlers live”

The main HTTP entry for a Python Worker is the fetch handler on a class named Default that extends WorkerEntrypoint—not top-level on_fetch functions unless you opt into the legacy style.

When you write Workers in Python, handlers are placed in a class named Default that extends the WorkerEntrypoint class (which you can import from the workers SDK module).

— Cloudflare, Handlers in Python Workers

A minimal Worker matches The Basics:

from workers import WorkerEntrypoint, Response
class Default(WorkerEntrypoint):
async def fetch(self, request):
return Response("Hello World!")

Handlers moved into an entrypoint class by default; legacy module-level handlers remain available with disable_python_no_global_handlers:

Previously, handlers were defined at the top-level of a module as on_fetch, on_scheduled, etc. methods, but now they live in an entrypoint class.

— Cloudflare, Python Workers handlers now live in an entrypoint class

The request object is a JavaScript Request exposed through the FFI, so patterns like await request.json() work from async fetch methods—as in Parse JSON from the incoming request. JSON responses use Response.json(data).

Pywrangler and pyproject.toml: local dev, packages, and deploy

Section titled “Pywrangler and pyproject.toml: local dev, packages, and deploy”

Pywrangler is the CLI for Python Workers: it wraps Wrangler, installs Workers-compatible wheels, vendors dependencies, and bundles them on deploy.

Pywrangler ↗︎ is a CLI tool for managing packages and Python Workers. It is meant as a wrapper for wrangler that sets up a full environment for you, including bundling your packages into your worker bundle on deployment.

— Cloudflare, Packages

Declare dependencies in pyproject.toml (runtime in dependencies, tooling in dependency-groups dev):

[project]
name = "YourProjectName"
version = "0.1.0"
requires-python = ">=3.13"
dependencies = [
"fastapi"
]
[dependency-groups]
dev = [
"workers-py",
"workers-runtime-sdk"
]

Typical commands (from Python Workers and Easy Python package management with Pywrangler):

  • Local: uv run pywrangler dev (or uvx --from workers-py pywrangler dev on Windows—see Better Windows support for Python Workers)
  • Deploy: uv run pywrangler deploy
  • New project: uvx --from workers-py pywrangler init

When you run uv run pywrangler dev, the runtime picks Pyodide from your compatibility date, installs packages from pyproject.toml, creates a V8 isolate, injects Pyodide, and serves your code—mirroring production without extra toolchain steps.

Concern Minimum versions (Windows Pywrangler)
wrangler ≥ 4.64.0
workers-py ≥ 1.72.0
uv ≥ 0.9.28

Supported libraries are pure Python, PyEmscripten wheels on PyPI, or packages bundled with Pyodide; not every PyPI package has a WASM wheel yet.

WSGI and ASGI in Python Workers: Django, Flask, and FastAPI

Section titled “WSGI and ASGI in Python Workers: Django, Flask, and FastAPI”

Python web frameworks that speak WSGI or ASGI can run in Python Workers via adapters in the workers module—they do not open sockets themselves; the Worker runtime acts as the gateway server.

Python web frameworks following the Web Server Gateway Interface (WSGI) ↗︎ or Asynchronous Server Gateway Interface (ASGI) ↗︎ specification can now be used in Python Workers.

— Cloudflare, Python Workers now support WSGI web frameworks like Django and Flask

WSGI (workers.wsgi): Flask and Django (WSGI mode)

Section titled “WSGI (workers.wsgi): Flask and Django (WSGI mode)”

For Flask or Django with get_wsgi_application():

from workers import wsgi
from django.core.wsgi import get_wsgi_application
app = get_wsgi_application()
Default = wsgi.entrypoint(app)

wsgi.entrypoint(app) is equivalent to a custom WorkerEntrypoint that calls wsgi.fetch(app, request, self.env); bindings are available on scope["env"] per Django on Python Workers.

ASGI (workers.asgi): FastAPI and Django (ASGI mode)

Section titled “ASGI (workers.asgi): FastAPI and Django (ASGI mode)”

For FastAPI:

from workers import asgi
from fastapi import FastAPI
app = FastAPI()
Default = asgi.entrypoint(app)

Django notes the runtime is optimized for ASGI, though WSGI remains supported. Django can use D1 or Durable Objects through the django-cf package (see the same Django doc).

FFI and bindings: D1, secrets, and JavaScript APIs from Python

Section titled “FFI and bindings: D1, secrets, and JavaScript APIs from Python”

The FFI bridges Pyodide Python to JavaScript so you can use Cloudflare bindings and globals (Request, Response, fetch()) from Python code.

Via Pyodide ↗︎, Python Workers provide a Foreign Function Interface (FFI) ↗︎ to JavaScript. This allows you to: Use bindings to resources on Cloudflare… Use JavaScript globals, like Request, Response, and fetch(). Use the full feature set of Cloudflare Workers — if an API is accessible in JavaScript, you can also access it in a Python Worker, writing exclusively Python code.

— Cloudflare, Foreign Function Interface (FFI)

Query D1 from Python Workers shows binding names must match Wrangler: await self.env.DB.prepare("…").run() when the D1 binding is named DB. Hyperdrive with Python Workers requires compatibility_date of 2026-09-08 or later; only synchronous SQLAlchemy ORMs are supported (async SQLAlchemy is not, due to missing greenlet support).

Prefer from workers import Response over from js import Response unless you need direct js access (FFI).

Durable Objects in Python Workers: stateful classes and Wrangler migrations

Section titled “Durable Objects in Python Workers: stateful classes and Wrangler migrations”

You can define Durable Objects in Python by subclassing DurableObject, wiring bindings and SQLite migrations in Wrangler, and routing from a WorkerEntrypoint.

You can now create Durable Objects using Python Workers. A Durable Object is a special kind of Cloudflare Worker which uniquely combines compute with storage, enabling stateful long-running applications which run close to your users.

— Cloudflare, Durable Objects are now supported in Python Workers

You need durable_objects.bindings, a migrations entry with new_sqlite_classes, and local testing via npx wrangler dev as shown in that changelog. Storage is accessible through self.ctx.storage.sql in the object class.

Python Workflows: multi-step durable jobs in Python

Section titled “Python Workflows: multi-step durable jobs in Python”

Python Workflows use WorkflowEntrypoint and the same step model as JavaScript Workflows—retries, persisted state, sleeps, and DAG dependencies via depends.

You can now build Workflows using Python. With Python Workflows, you get automatic retries, state persistence, and the ability to run multi-step operations that can span minutes, hours, or weeks using Python’s familiar syntax and the Python Workers runtime.

— Cloudflare, Build durable multi-step applications in Python with Workflows (now in beta)

Enable both python_workflows and python_workers in Wrangler; configure a workflows binding (Interact with a Workflow). Changelog notes require compatibility_date = "2025-08-01" or lower in wrangler for Python Workflows—whereas the Workflows Python binding doc states 2025-08-01 or later; align flags, dates, and Python Workflows SDK examples when you ship. Trigger creation from a Worker with await self.env.MY_WORKFLOW.create().

Deploy-time memory snapshots and cold start performance

Section titled “Deploy-time memory snapshots and cold start performance”

On deploy, Cloudflare runs top-level imports and captures a memory snapshot so new isolates avoid reloading Pyodide and heavy imports on every cold start—described in Deployment Lifecycle and Cold Start Optimizations and Python cold start improvements.

Every time you deploy a Python Worker, a memory snapshot is captured after the top level of the Worker is executed. This snapshot captures all imports, including package imports that are often costly to load.

— Cloudflare, Python cold start improvements

Published benchmark (imports of httpx, fastapi, pydantic): mean cold start about 1027 ms on Cloudflare Python Workers vs 2502 ms (AWS Lambda) and 3069 ms (Google Cloud Run); without snapshots, the same benchmark was about 10 seconds—roughly a tenfold improvement. Keep expensive work at module top level only when you want it in the snapshot; avoid unintended side effects on import.

Pyodide and Python versions: compatibility date and Python 3.14

Section titled “Pyodide and Python versions: compatibility date and Python 3.14”

New Pyodide/Python versions are gated by compatibility date and flags. Workers with compatibility_date 2026-09-08 or later default to Python 3.14 (Pyodide 314.0.6)—see Python 3.14 for Python Workers.

How Python Workers Work explains the yearly Python release cadence, five-year support windows, and that older pinned versions may keep running in production but are not recommended for new projects.

Standard library limits and common mistakes

Section titled “Standard library limits and common mistakes”

The Standard Library is largely available under Pyodide, but OS-specific modules (fcntl, termios, tkinter, venv, and others listed there) are missing. multiprocessing and threading import but are not functional in the WASM VM; pty/tty depend on removed termios.

Mistake What docs recommend
No python_workers flag Add compatibility_flags = ["python_workers"]
Top-level on_fetch without legacy flag Use Default(WorkerEntrypoint) or set disable_python_no_global_handlers
Binding name mismatch Wrangler binding name must equal self.env.<NAME> (e.g. D1 DB)
Workflows only with python_workers Add python_workflows and workflow bindings in Wrangler
Expecting async SQLAlchemy on Hyperdrive Only synchronous SQLAlchemy ORMs are supported
Assuming every PyPI package works Need pure Python, PyEmscripten wheel, or Pyodide bundle

More patterns: Python examples and the python-workers-examples repository (git clone https://github.com/cloudflare/python-workers-examples).

  1. Confirm compatibility_flags includes python_workers (and python_workflows if you use Workflows) in the Wrangler file, with a compatibility_date appropriate for your target Python version—see Compatibility flags and Python 3.14 for Python Workers.

  2. Run uv run pywrangler dev and exercise routes (plain fetch, or WSGI/ASGI apps per Flask / FastAPI docs).

  3. Deploy with uv run pywrangler deploy and hit the published workers.dev URL; for D1, verify binding output matches Query D1 from Python Workers.

  4. If you use Durable Objects, run npx wrangler dev locally after migrations are defined, as in Durable Objects are now supported in Python Workers.

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact sentence you are asking about so it can be corrected.