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

PyCharm调试模式导入FastAPI TestClient耗时24秒的原因排查

调试模式下导入FastAPI TestClient异常缓慢的优化问题

使用PyCharm Pro时,运行模式导入fastapi.testclient.TestClient仅需约0.87秒,但调试模式下耗时长达24.22秒,不确定问题出在PyCharm、FastAPI、Starlette还是其他环节,求优化方向。


虚拟环境已安装包

> uv pip list
Package           Version
----------------- --------
annotated-types   0.7.0
anyio             4.4.0
certifi           2024.6.2
click             8.1.7
colorama          0.4.6
dnspython         2.6.1
email-validator   2.2.0
fastapi           0.111.0
fastapi-cli       0.0.4
h11               0.14.0
httpcore          1.0.5
httptools         0.6.1
httpx             0.27.0
idna              3.7
iniconfig         2.0.0
jinja2            3.1.4
markdown-it-py    3.0.0
markupsafe        2.1.5
mdurl             0.1.2
orjson            3.10.5
packaging         24.1
pluggy            1.5.0
pydantic          2.7.4
pydantic-core     2.18.4
pygments          2.18.0
pytest            8.2.2
python-dotenv     1.0.1
python-multipart  0.0.9
pyyaml            6.0.1
rich              13.7.1
shellingham       1.5.4
sniffio           1.3.1
starlette         0.37.2
typer             0.12.3
typing-extensions 4.12.2
ujson             5.10.0
uvicorn           0.30.1
watchfiles        0.22.0
websockets        12.0

测试代码

import time

START_TIME = time.time()
def test_debugger_speed():
    end_time = time.time()
    print(f"Time taken for test_debugger_speed: {end_time - START_TIME} seconds")


def test_debugger_speed_with_client():
    start_time = time.time()
    from fastapi.testclient import TestClient
    end_time = time.time()
    print(f"Time taken for importing TestClient: {end_time - start_time} seconds")

运行模式输出

C:\Users\RYZEN\prdev\pythonProject1\.venv\Scripts\python.exe "C:/Program Files/JetBrains/PyCharm 2024.1.3/plugins/python/helpers/pycharm/_jb_pytest_runner.py" --path C:\Users\RYZEN\prdev\pythonProject1\test_slow_debugger.py 
Testing started at 3:12 PM ...
Launching pytest with arguments C:\Users\RYZEN\prdev\pythonProject1\test_slow_debugger.py --no-header --no-summary -q in C:\Users\RYZEN\prdev\pythonProject1

============================= test session starts =============================
collecting ... collected 2 items

test_slow_debugger.py::test_debugger_speed PASSED                        [ 50%]Time taken for test_debugger_speed: 0.00400090217590332 seconds

test_slow_debugger.py::test_debugger_speed_with_client PASSED            [100%]Time taken for importing TestClient: 0.8693747520446777 seconds


============================== 2 passed in 0.90s ==============================

Process finished with exit code 0

调试模式输出

C:\Users\RYZEN\prdev\pythonProject1\.venv\Scripts\python.exe -X pycache_prefix=C:\Users\RYZEN\AppData\Local\JetBrains\PyCharm2024.1\cpython-cache "C:/Program Files/JetBrains/PyCharm 2024.1.3/plugins/python/helpers/pydev/pydevd.py" --multiprocess --qt-support=auto --client 127.0.0.1 --port 1842 --file "C:/Program Files/JetBrains/PyCharm 2024.1.3/plugins/python/helpers/pycharm/_jb_pytest_runner.py" --path C:\Users\RYZEN\prdev\pythonProject1\test_slow_debugger.py
Testing started at 3:12 PM ...
Connected to pydev debugger (build 241.17890.14)
Launching pytest with arguments C:\Users\RYZEN\prdev\pythonProject1\test_slow_debugger.py --no-header --no-summary -q in C:\Users\RYZEN\prdev\pythonProject1

============================= test session starts =============================
collecting ... collected 2 items

test_slow_debugger.py::test_debugger_speed PASSED                        [ 50%]Time taken for test_debugger_speed: 0.019998788833618164 seconds

test_slow_debugger.py::test_debugger_speed_with_client PASSED            [100%]Time taken for importing TestClient: 24.218650102615356 seconds


============================= 2 passed in 24.30s ==============================

Process finished with exit code 0

优化方向

  • 调整PyCharm调试器设置:
    • 关闭「自动附加子进程」选项,避免调试器额外处理TestClient导入触发的子进程
    • 在「运行/调试配置」→「调试器」→「跳过文件」中添加fastapi/*、starlette/*、httpx/*等第三方库路径,跳过对这些模块的调试追踪
    • 禁用「收集运行时类型信息」选项,减少导入时的类型分析开销
  • 优化导入逻辑:
    • 将from fastapi.testclient import TestClient移到测试文件顶部全局导入,避免每次调试测试时重复触发导入
    • 删除调试模式的自定义pycache缓存目录,改用默认缓存路径,减少缓存读取开销
  • 排查依赖问题:
    • 尝试降级FastAPI/Starlette到稳定版本(如FastAPI 0.109.x),新版本可能引入了更多调试器需要追踪的模块
    • 卸载测试非必需的依赖(如dnspython、email-validator),排查是否有冗余模块拖慢导入
  • 替代调试方式:
    • 使用PyCharm的「Python控制台运行」替代pytest调试模式,减少调试器的额外负载

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 05:25:18