Cloudflare Workers Python SDK
Python SDK for building Cloudflare Workers. Allows writing serverless functions in Python that run on the Cloudflare edge network using Pyodide (CPython compiled to WebAssembly). This is distinct from the 'cloudflare' package which is the API client for managing Cloudflare resources.
Warnings
- breaking The import path is 'cloudflare.workers', NOT 'cloudflare_workers'. The pip package name uses a hyphen but the import namespace uses a dot.
- gotcha This package conflicts with the 'cloudflare' PyPI package (the Cloudflare API client). Both occupy the 'cloudflare' namespace. Do not install both in the same environment.
- gotcha Python Workers run on Pyodide (CPython in WebAssembly). Not all Python packages are available. Only pure-Python packages and select packages with Pyodide builds are supported.
- gotcha The handler function must be async. Synchronous handlers will fail silently or raise errors at runtime.
- gotcha Python Workers require compatibility_flags = ["python_workers"] in wrangler.toml and main must point to a .py file. Without this flag, deployment fails.
- deprecated Python Workers support is still in open beta. APIs may change between releases without following semver strictly.
Install
-
pip install cloudflare-workers
Imports
- Response
from cloudflare.workers import Response
- Request
from cloudflare.workers import Request
- fetch
from cloudflare.workers import fetch
Quickstart
from cloudflare.workers import Response
async def on_fetch(request, env):
return Response('Hello from Python Workers!')