|
| 1 | +--- |
| 2 | +pcx_content_type: concept |
| 3 | +title: The Basics |
| 4 | +sidebar: |
| 5 | + order: 4 |
| 6 | +head: |
| 7 | + - tag: title |
| 8 | + content: Learn the basics of Python Workers |
| 9 | +description: Learn the basics of Python Workers |
| 10 | +--- |
| 11 | + |
| 12 | +import { WranglerConfig } from "~/components"; |
| 13 | + |
| 14 | +## Fetch Handler |
| 15 | + |
| 16 | +As mentioned in the [introduction to Python Workers](/workers/languages/python/), a Python Worker can be as simple as four lines of code: |
| 17 | + |
| 18 | +```python |
| 19 | +from workers import WorkerEntrypoint, Response |
| 20 | + |
| 21 | +class Default(WorkerEntrypoint): |
| 22 | + async def fetch(self, request): |
| 23 | + return Response("Hello World!") |
| 24 | +``` |
| 25 | + |
| 26 | +Similar to other Workers, the main entry point for a Python worker is the [`fetch` handler](/workers/runtime-apis/handlers/fetch) which handles incoming requests |
| 27 | +sent to the Worker. |
| 28 | + |
| 29 | +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). |
| 30 | + |
| 31 | +## The `Request` Interface |
| 32 | + |
| 33 | +The `request` parameter passed to your `fetch` handler is a JavaScript Request object, exposed via the [foreign function interface (FFI)](/workers/languages/python/ffi), |
| 34 | +allowing you to access it directly from your Python code. |
| 35 | + |
| 36 | +Let's try editing the worker to accept a POST request. We know from the |
| 37 | +[documentation for `Request`](/workers/runtime-apis/request) that we can call |
| 38 | +`await request.json()` within an `async` function to parse the request body as |
| 39 | +JSON. |
| 40 | + |
| 41 | +In a Python Worker, you would write: |
| 42 | + |
| 43 | +```python |
| 44 | +from workers import WorkerEntrypoint, Response |
| 45 | +from hello import hello |
| 46 | + |
| 47 | +class Default(WorkerEntrypoint): |
| 48 | + async def fetch(self, request): |
| 49 | + name = (await request.json()).name |
| 50 | + return Response(hello(name)) |
| 51 | +``` |
| 52 | + |
| 53 | +Many other JavaScript APIs are available in Python Workers via the FFI, so you can |
| 54 | +call other methods in a similar way. |
| 55 | + |
| 56 | +Once you edit the `src/entry.py`, Wrangler will automatically restart the local |
| 57 | +development server. |
| 58 | + |
| 59 | +Now, if you send a POST request with the appropriate body, |
| 60 | +your Worker will respond with a personalized message. |
| 61 | + |
| 62 | +```bash |
| 63 | +curl --header "Content-Type: application/json" \ |
| 64 | + --request POST \ |
| 65 | + --data '{"name": "Python"}' http://localhost:8787 |
| 66 | +``` |
| 67 | + |
| 68 | +```bash output |
| 69 | +Hello, Python! |
| 70 | +``` |
| 71 | + |
| 72 | +## The `env` Attribute |
| 73 | + |
| 74 | +The `env` attribute on the `WorkerEntrypoint` can be used to access |
| 75 | +[environment variables](/workers/configuration/environment-variables/), |
| 76 | +[secrets](/workers/configuration/secrets/),and |
| 77 | +[bindings](/workers/runtime-apis/bindings/). |
| 78 | + |
| 79 | +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](/workers/wrangler/configuration/): |
| 80 | + |
| 81 | +<WranglerConfig> |
| 82 | + |
| 83 | +```toml |
| 84 | +name = "hello-python-worker" |
| 85 | +main = "src/entry.py" |
| 86 | +compatibility_flags = ["python_workers"] |
| 87 | +compatibility_date = "2025-11-02" |
| 88 | + |
| 89 | +[vars] |
| 90 | +API_HOST = "example.com" |
| 91 | +``` |
| 92 | + |
| 93 | +</WranglerConfig> |
| 94 | + |
| 95 | +Then, you can access the `API_HOST` environment variable via the `env` parameter: |
| 96 | + |
| 97 | +```python |
| 98 | +from workers import WorkerEntrypoint, Response |
| 99 | + |
| 100 | +class Default(WorkerEntrypoint): |
| 101 | + async def fetch(self, request): |
| 102 | + return Response(self.env.API_HOST) |
| 103 | +``` |
| 104 | + |
| 105 | +## Modules |
| 106 | + |
| 107 | +Python workers can be split across multiple files. |
| 108 | + |
| 109 | +Let's create a new Python file, called `src/hello.py`: |
| 110 | + |
| 111 | +```python |
| 112 | +def hello(name): |
| 113 | + return "Hello, " + name + "!" |
| 114 | +``` |
| 115 | + |
| 116 | +Now, we can modify `src/entry.py` to make use of the new module. |
| 117 | + |
| 118 | +```python |
| 119 | +from hello import hello |
| 120 | +from workers import WorkerEntrypoint, Response |
| 121 | + |
| 122 | +class Default(WorkerEntrypoint): |
| 123 | + async def fetch(self, request): |
| 124 | + return Response(hello("World")) |
| 125 | +``` |
| 126 | + |
| 127 | +Once you edit `src/entry.py`, [`pywrangler`](/workers/languages/python/#the-pywrangler-cli-tool) will automatically detect the change and |
| 128 | +reload your Worker. |
| 129 | + |
| 130 | +## Types and Autocompletion |
| 131 | + |
| 132 | +When developing Python Workers, you can take advantage of type hints and autocompletion |
| 133 | +in your IDE. |
| 134 | + |
| 135 | +To enable them, install the `workers-runtime-sdk` package in your `pyproject.toml` file. |
| 136 | + |
| 137 | +```toml |
| 138 | +[dependency-groups] |
| 139 | +dev = [ |
| 140 | + "workers-py", |
| 141 | + "workers-runtime-sdk" |
| 142 | +] |
| 143 | +``` |
| 144 | + |
| 145 | +Additionally, you can generate types based on your Worker configuration using `uv run pywrangler types` |
| 146 | + |
| 147 | +This includes Env types based on your bindings, module rules, and runtime types based on the compatibility_date |
| 148 | +and compatibility_flags in your config file. |
| 149 | + |
| 150 | +## Upgrading `pywrangler` |
| 151 | + |
| 152 | +To upgrade to the latest version of [`pywrangler`](/workers/languages/python/#the-pywrangler-cli-tool) globally, run the following command: |
| 153 | + |
| 154 | +```bash |
| 155 | +uv tool upgrade workers-py |
| 156 | +``` |
| 157 | + |
| 158 | +To upgrade to the latest version of `pywrangler` in a specific project, run the following command: |
| 159 | + |
| 160 | +```bash |
| 161 | +uv lock --upgrade-package workers-py |
| 162 | +``` |
| 163 | + |
| 164 | +## Next Up |
| 165 | + |
| 166 | +- Learn details about local development, deployment, and [how Python Workers work](/workers/languages/python/how-python-workers-work). |
| 167 | +- Explore the [package](/workers/languages/python/packages) docs for instructions on how to use packages with Python Workers. |
| 168 | +- Understand which parts of the [Python Standard Library](/workers/languages/python/stdlib) are supported in Python Workers. |
| 169 | +- Learn about Python Workers' [foreign function interface (FFI)](/workers/languages/python/ffi), and how to use it to work with [bindings](/workers/runtime-apis/bindings) and [Runtime APIs](/workers/runtime-apis/). |
0 commit comments