Thanks to visit codestin.com
Credit goes to developers.cloudflare.com

Skip to content
Cloudflare Docs

Python Workers

Cloudflare Workers provides a first-class Python experience, including support for:

Introduction

A Python Worker can be as simple as four lines of code:

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

Similar to other Workers, the main entry point for a Python worker is the fetch handler which handles incoming requests sent to the Worker.

In a Python Worker, this handler is placed in a Default class that extends the WorkerEntrypoint class (which you can import from the workers SDK module).

The pywrangler CLI tool

To run a Python Worker locally, install packages, and deploy it to Cloudflare, you use pywrangler, the CLI for Python Workers.

To set it up, first, ensure uv and Node are installed.

Then set up your development environment:

uv init
uv tool install workers-py
uv run pywrangler init

This will create a pyproject.toml file with workers-py as a development dependency. pywrangler init will create a wrangler config file. You can then run pywrangler with:

Terminal window
uv run pywrangler dev

To deploy a Python Worker to Cloudflare, run pywrangler deploy:

Terminal window
uv run pywrangler deploy

Python Worker Templates

When you initialize a new Python Worker project and select from one of many templates:

Terminal window
uv run pywrangler init

Or you can clone the examples repository to explore more options:

Terminal window
git clone https://github.com/cloudflare/python-workers-examples
cd python-workers-examples/01-hello

Next Up