workerd
workerd copied to clipboard
π BUG: python FastAPI worker: ModuleNotFoundError: No module named 'httpx'
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 <module>
import httpx
ModuleNotFoundError: No module named 'httpx'
at null.<anonymous> (pyodide-internal:generated/pyodide.asm:20:9998) in new_error
at [object Object] in $wrap_exception
at [object Object] in $pythonexc2js
at null.<anonymous> (pyodide-internal:generated/pyodide.asm:20:607956) in Module._pythonexc2js
at null.<anonymous> (pyodide-internal:generated/pyodide.asm:20:64098) in
Module.callPyObjectKwargs
at null.<anonymous> (pyodide-internal:generated/pyodide.asm:20:65055) in Module.callPyObject
at null.<anonymous> (pyodide-internal:generated/pyodide.asm:20:79132) in apply
at null.<anonymous> (pyodide-internal:generated/pyodide.asm:20:77371) in apply
at null.<anonymous> (pyodide-internal:generated/pyodide.asm:20:102862) in pyimport
at null.<anonymous> (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
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 <module>
import httpx
ModuleNotFoundError: No module named 'httpx'
at null.<anonymous> (pyodide-internal:generated/pyodide.asm:20:9998) in new_error
at [object Object] in $wrap_exception
at [object Object] in $pythonexc2js
at null.<anonymous> (pyodide-internal:generated/pyodide.asm:20:607956) in Module._pythonexc2js
at null.<anonymous> (pyodide-internal:generated/pyodide.asm:20:64098) in
Module.callPyObjectKwargs
at null.<anonymous> (pyodide-internal:generated/pyodide.asm:20:65055) in Module.callPyObject
at null.<anonymous> (pyodide-internal:generated/pyodide.asm:20:79132) in apply
at null.<anonymous> (pyodide-internal:generated/pyodide.asm:20:77371) in apply
at null.<anonymous> (pyodide-internal:generated/pyodide.asm:20:102862) in pyimport
at null.<anonymous> (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
What does your requirements.txt file look like? It doesn't look like you have one defined
Yes, because requirements.txt is not supported now, I did not add the requirements.txt file.
requirements.txt is the only way we support these packages, they cannot be deployed yet but it will be possible soon.
Thank you for the reply. I hope to use requirements.txt to deploy the project as soon as possible. Do you have a specific timeline? A month, six months, or a year?
@dom96 Seconding @yym68686 question. Any broad timeline ?
@dom96 Seconding @yym68686 question. Any broad timeline ?
@dom96 does this mean that we cannot deploy fastapi apps in the example at the moment?
Hey @yym68686, @mmabrouk, & @afeezaziz,
Regarding timing on package support, I think a very rough estimate would be something like 6 months for open beta package support in prod. We donβt have a firm date, but in any case, I wouldnβt build something today with the expectation that this is just around the corner.
Some context is that current Python startup times with packages are long enough where we think cold starts would impact end-users regularly in a non-trivial way. We are working on two things: reducing cold start length and reducing cold start frequency for Python. Weβve got a good plan on both (pre-initializing isolates with Pyodide and loading package memory snapshots to reduce time & smartly routing requests to re-use Workers that are already loaded), but itβll take some time to execute on this.
Sorry if that timeline is disappointing. It is longer than we wanted it to be on our end as well.
Thanks for the quick answer @mikenomitch ! Looking forward to it. I think if you get this right, cloudflare could become the place to host genAI applications.
@mikenomitch is there any update on the open beta?
Is there any new progress?
They are running a close beta program for packages in production. You need to apply by submitting a form. They are supporting 50+ packages rn, and other packages can be vendored.
Check out this message on Discord: https://discord.com/channels/595317990191398933/1224715939581530112/1351242879061856388
https://blog.cloudflare.com/python-workers/
https://developers.cloudflare.com/workers/languages/python/packages/