cloudflare / cloudflare/workerd

πŸ› BUG: python FastAPI worker: ModuleNotFoundError: No module named 'httpx'

Open
#2,396 12 comments 0 reactions 0 assignees View on GitHub
bug python
Dominant language
C++
Stars
8.7k
Forks
739
Avg merge
2d 20h
Merged PRs (30d)
174

Description

### Which Cloudflare product(s) does this pertain to?

Workers Runtime

### What version(s) of the tool(s) are you using?

wrangler 3.63.2

### What version of Node are you using?

v20.13.1

### What operating system and version are you using?

Mac 14.4.1

### Describe the Bug

### Observed behavior

display

```
✘ [ERROR] A request to the Cloudflare API (/accounts/8ec0d42e9131e125b020b6d4918fe721/workers/scripts/uni-api) failed.

Uncaught Error: PythonError: Traceback (most recent call last):
File "/lib/python312.zip/_pyodide/_base.py", line 629, in pyimport_impl
res = __import__(stem, fromlist=fromlist)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/session/metadata/main.py", line 3, in
import httpx
ModuleNotFoundError: No module named 'httpx'

at null. (pyodide-internal:generated/pyodide.asm:20:9998) in new_error
at [object Object] in $wrap_exception
at [object Object] in $pythonexc2js
at null. (pyodide-internal:generated/pyodide.asm:20:607956) in Module._pythonexc2js
at null. (pyodide-internal:generated/pyodide.asm:20:64098) in
Module.callPyObjectKwargs
at null. (pyodide-internal:generated/pyodide.asm:20:65055) in Module.callPyObject
at null. (pyodide-internal:generated/pyodide.asm:20:79132) in apply
at null. (pyodide-internal:generated/pyodide.asm:20:77371) in apply
at null. (pyodide-internal:generated/pyodide.asm:20:102862) in pyimport
at null. (pyodide:python-entrypoint-helper:31:18) in pyimportMainModule
[code: 10021]


If you think this is a bug, please open an issue at:
https://github.com/cloudflare/workers-sdk/issues/new/choose
```

### Expected behavior

No error, can import the httpx module

### Steps to reproduce
Please provide the following:
- A minimal working subset of your worker code

```python
import httpx
from contextlib import asynccontextmanager
from fastapi import FastAPI

@asynccontextmanager
async def lifespan(app: FastAPI):
timeout = httpx.Timeout(connect=10.0, read=30.0, write=30.0, pool=30.0)
app.state.client = httpx.AsyncClient(timeout=timeout)
yield
await app.state.client.aclose()

app = FastAPI(lifespan=lifespan)

@app.get("/generate-api-key")
def generate_api_key():
return {"api_key": "hello world"}

async def on_fetch(request, env):
import asgi

return await asgi.fetch(app, request, env)
```
- A minimal working subset of your `wrangler.toml`
```
name = "uni-api"
main = "main.py"
compatibility_flags = ["python_workers"]
compatibility_date = "2024-03-20"
```
- Commands used to start your local dev server, including custom env and cli args
```
wrangler deploy
```
- Steps to be performed in the browser, curl commands, or a test we can run that reliably fails (at least a percent of the time)

An error occurred immediately after executing wrangler deploy.

A git repo we can clone and run a test suite on, or which has a README with step-by-step instructions, is even better. In this case, please use the field below to provide a link to the minimal repro.

### Please provide a link to a minimal reproduction

_No response_

### Please provide any relevant error logs

```
⛅️ wrangler 3.63.2
-------------------

β–² [WARNING] The entrypoint main.py defines a Python worker, support for Python workers is currently experimental.

Attaching additional modules:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Name β”‚ Type β”‚ Size β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ .wrangler/tmp/deploy-dpY4MJ/main.py β”‚ python β”‚ 0.58 KiB β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ main copy.py β”‚ python β”‚ 9.98 KiB β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ models.py β”‚ python β”‚ 1.20 KiB β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ request.py β”‚ python β”‚ 8.74 KiB β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ response.py β”‚ python β”‚ 7.15 KiB β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ test/launch_with_clear.py β”‚ python β”‚ 0.18 KiB β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Total Upload: 28.40 KiB / gzip: 6.28 KiB

✘ [ERROR] A request to the Cloudflare API (/accounts/8ec0d42e9131e125b020b6d4918fe721/workers/scripts/uni-api) failed.

Uncaught Error: PythonError: Traceback (most recent call last):
File "/lib/python312.zip/_pyodide/_base.py", line 629, in pyimport_impl
res = __import__(stem, fromlist=fromlist)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/session/metadata/main.py", line 1, in
import httpx
ModuleNotFoundError: No module named 'httpx'

at null. (pyodide-internal:generated/pyodide.asm:20:9998) in new_error
at [object Object] in $wrap_exception
at [object Object] in $pythonexc2js
at null. (pyodide-internal:generated/pyodide.asm:20:607956) in Module._pythonexc2js
at null. (pyodide-internal:generated/pyodide.asm:20:64098) in
Module.callPyObjectKwargs
at null. (pyodide-internal:generated/pyodide.asm:20:65055) in Module.callPyObject
at null. (pyodide-internal:generated/pyodide.asm:20:79132) in apply
at null. (pyodide-internal:generated/pyodide.asm:20:77371) in apply
at null. (pyodide-internal:generated/pyodide.asm:20:102862) in pyimport
at null. (pyodide:python-entrypoint-helper:31:18) in pyimportMainModule
[code: 10021]


If you think this is a bug, please open an issue at:
https://github.com/cloudflare/workers-sdk/issues/new/choose
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.