Python
Cloudflare Workers provides first-class support for Python, including support for:
- The majority of Python's Standard library
- All bindings, including Workers AI, Vectorize, R2, KV, D1, Queues, Durable Objects, Service Bindings and more.
- Environment Variables, and Secrets
- A robust foreign function interface (FFI) that lets you use JavaScript objects and functions directly from Python — including all Runtime APIs
- Built-in packages, including FastAPI ↗, Langchain ↗, httpx ↗ and more.
You need uv ↗ and Node.
git clone https://github.com/cloudflare/python-workers-examplescd python-workers-examples/01-hellouv run pywrangler dev
A Python Worker can be as simple as three lines of code:
from workers import WorkerEntrypoint, Response
class Default(WorkerEntrypoint): async def fetch(self, request): return Response("Hello World!")
Similar to Workers written in JavaScript, TypeScript, or Rust, the main entry point for a Python worker is the fetch
handler. 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).
To run a Python Worker locally, you use pywrangler, the CLI for Python Workers. To set it up, first you need to set up your development environment:
uv inituv tool install workers-pyuv 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:
uv run pywrangler dev
To deploy a Python Worker to Cloudflare, run pywrangler deploy
:
uv run pywrangler deploy
Python workers can be split across multiple files. Let's create a new Python file, called src/hello.py
:
def hello(name): return "Hello, " + name + "!"
Now, we can modify src/entry.py
to make use of the new module.
from hello import hellofrom workers import WorkerEntrypoint, Response
class Default(WorkerEntrypoint): async def fetch(self, request): return Response(hello("World"))
Once you edit src/entry.py
, Wrangler will automatically detect the change and
reload your Worker.
The request
parameter passed to your fetch
handler is a JavaScript Request object, exposed via the foreign function interface, allowing you to access it directly from your Python code.
Let's try editing the worker to accept a POST request. We know from the
documentation for Request
that we can call
await request.json()
within an async
function to parse the request body as
JSON. In a Python Worker, you would write:
from workers import WorkerEntrypoint, Responsefrom hello import hello
class Default(WorkerEntrypoint): async def fetch(self, request): name = (await request.json()).name return Response(hello(name))
Once you edit the src/entry.py
, Wrangler should automatically restart the local
development server. Now, if you send a POST request with the appropriate body,
your Worker should respond with a personalized message.
curl --header "Content-Type: application/json" \ --request POST \ --data '{"name": "Python"}' http://localhost:8787
Hello, Python!
The env
attribute on the WorkerEntrypoint
can be used to access
environment variables,
secrets,and
bindings.
For example, let us try setting and using an environment variable in a Python Worker. First, add the environment variable to your Worker's Wrangler configuration file:
{ "name": "hello-python-worker", "main": "src/entry.py", "compatibility_flags": [ "python_workers" ], "compatibility_date": "2024-03-20", "vars": { "API_HOST": "example.com" }}
name = "hello-python-worker"main = "src/entry.py"compatibility_flags = ["python_workers"]compatibility_date = "2024-03-20"
[vars]API_HOST = "example.com"
Then, you can access the API_HOST
environment variable via the env
parameter:
from workers import WorkerEntrypoint, Response
class Default(WorkerEntrypoint): async def fetch(self, request): return Response(self.env.API_HOST)
- Understand which parts of the Python Standard Library are supported in Python Workers.
- Learn about Python Workers' foreign function interface (FFI), and how to use it to work with bindings and Runtime APIs.
- Explore the packages docs for instructions on how to use packages with Python Workers.
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Directory
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark
-