|
8 | 8 |
|
9 | 9 | import { Render } from "~/components"; |
10 | 10 |
|
11 | | -<Render file="python-workers-beta-packages" product="workers" /> |
| 11 | +## Using pywrangler |
12 | 12 |
|
13 | | -To import a Python package, add the package name to the `requirements.txt` file within the same directory as your [Wrangler configuration file](/workers/wrangler/configuration/). |
| 13 | +[Pywrangler](https://github.com/cloudflare/workers-py?tab=readme-ov-file#pywrangler) is a CLI tool for managing packages in a Python Workers |
| 14 | +project. By default, it will vendor any packages you have defined in your pyproject.toml file. |
| 15 | + |
| 16 | +To get started, create a pyproject.toml file with the following contents: |
| 17 | + |
| 18 | +``` |
| 19 | +[project] |
| 20 | +name = "YourProjectName" |
| 21 | +version = "0.1.0" |
| 22 | +description = "Add your description here" |
| 23 | +requires-python = ">=3.12" |
| 24 | +dependencies = [ |
| 25 | + "fastapi" |
| 26 | +] |
| 27 | +
|
| 28 | +[dependency-groups] |
| 29 | +dev = ["workers-py"] |
| 30 | +``` |
| 31 | + |
| 32 | +The above will allow your worker to depend on the [FastAPI](https://fastapi.tiangolo.com/) package. |
| 33 | + |
| 34 | +You can then run `uv run pywrangler sync` to install the packages in your project, in such a way so that they will be deployed along with your |
| 35 | +Worker. |
| 36 | + |
| 37 | +The `pywrangler` CLI also supports all commands supported by the `wrangler` tool, so you can use it to deploy your Worker, add environment variables, etc. |
| 38 | + |
| 39 | +For example, to run your Worker locally, you can run `uv run pywrangler dev`. This will automatically run `sync` for you, so you don't need to do so manually. |
| 40 | + |
| 41 | +## Built-in packages |
| 42 | + |
| 43 | +:::caution[Explicitly defined built-in packages are deprecated] |
| 44 | + |
| 45 | +You can still specify packages in a `cf-requirements.txt` file, but doing so is deprecated. Instead use `pywrangler` to add packages to your Worker as described above. |
| 46 | +::: |
| 47 | + |
| 48 | +To import a Python package, add the package name to the `cf-requirements.txt` file within the same directory as your [Wrangler configuration file](/workers/wrangler/configuration/). |
14 | 49 |
|
15 | 50 | For example, if your Worker depends on [FastAPI](https://fastapi.tiangolo.com/), you would add the following: |
16 | 51 |
|
17 | 52 | ``` |
18 | 53 | fastapi |
19 | 54 | ``` |
20 | 55 |
|
21 | | -## Package versioning |
| 56 | +### Package versioning |
22 | 57 |
|
23 | | -In the example above, you likely noticed that there is no explicit version of the Python package declared in `requirements.txt`. |
| 58 | +In the example above, you likely noticed that there is no explicit version of the Python package declared in `cf-requirements.txt`. |
24 | 59 |
|
25 | 60 | In Workers, Python package versions are set via [Compatibility Dates](/workers/configuration/compatibility-dates/) and [Compatibility Flags](/workers/configuration/compatibility-flags/). Given a particular compatibility date, a specific version of the [Pyodide Python runtime](https://pyodide.org/en/stable/project/changelog.html) is provided to your Worker, providing a specific set of Python packages pinned to specific versions. |
26 | 61 |
|
27 | 62 | As new versions of Pyodide and additional Python packages become available in Workers, we will publish compatibility flags and their associated compatibility dates here on this page. |
28 | 63 |
|
29 | | -## Supported Packages |
| 64 | +### Supported Packages |
30 | 65 |
|
31 | 66 | A subset of the [Python packages that Pyodide supports](https://pyodide.org/en/latest/usage/packages-in-pyodide.html) are provided directly by the Workers runtime: |
32 | 67 |
|
@@ -80,6 +115,6 @@ A subset of the [Python packages that Pyodide supports](https://pyodide.org/en/l |
80 | 115 |
|
81 | 116 | Looking for a package not listed here? Tell us what you'd like us to support by [opening a discussion on Github](https://github.com/cloudflare/workerd/discussions/new?category=python-packages). |
82 | 117 |
|
83 | | -## HTTP Client Libraries |
| 118 | +### HTTP Client Libraries |
84 | 119 |
|
85 | 120 | Only HTTP libraries that are able to make requests asynchronously are supported. Currently, these include [`aiohttp`](https://docs.aiohttp.org/en/stable/index.html) and [`httpx`](https://www.python-httpx.org/). You can also use the [`fetch()` API](/workers/runtime-apis/fetch/) from JavaScript, using Python Workers' [foreign function interface](/workers/languages/python/ffi) to make HTTP requests. |
0 commit comments