You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FastAPI接口测试报错:module 'anyio' has no attribute 'start_blocking_portal'

问题解决:TestClient调用时AttributeError: anyio无start_blocking_portal属性

错误原因

anyio.start_blocking_portal 是anyio 3.0及以上版本才提供的API,你的Python环境中安装的anyio版本低于3.0,导致Starlette的TestClient无法找到该方法。

解决方案

1. 升级anyio到兼容版本

执行以下命令升级:

pip install --upgrade anyio

2. 固定依赖版本(可选)

为了避免后续环境出现类似问题,在requirements.txt中添加anyio的版本约束:

black
fastapi==0.85.0
isort
pytest
pytest-regressions
requests
uvicorn
anyio>=3.4.0

注:Starlette 0.20.x(对应FastAPI 0.85.x)要求anyio最低版本为3.4.0,指定该版本可确保完全兼容

3. 验证安装结果

运行以下命令确认anyio版本符合要求:

pip show anyio

输出中Version字段应≥3.0.0,推荐≥3.4.0。

内容的提问来源于stack exchange,提问作者Potis23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 21:27:05