快速结论:该报错主要发生在 Windows 上运行 langchain-core 单元测试时,根因是子进程使用了 PATH 中的基础 Python 解释器(缺少 uuid_utils 模块),以及 NamedTemporaryFile 在 Windows 下保持文件锁导致无法重新打开。优先排查测试代码是否使用 sys.executable 而不是裸 python 命令,以及是否用 pytest 的 tmp_path 替代 NamedTemporaryFile。
适用环境:Windows 操作系统;langchain-core 包(Issue 未提供具体 Python 版本、CUDA、显卡信息,不做补充)。
最快修复方案:采用 PR #39664 的修复:在 test_importable_all_via_subprocess 中使用 sys.executable 启动子进程;在 test_from_file_encoding 中使用 pytest 的 tmp_path 夹具并配合 Path.write_text 写入文件。该方案已在 Issue 中确认合并并通过完整测试(2218 passed)。
注意事项:该修复仅针对测试代码的可移植性,不涉及 LangChain 运行时行为;如果用户在自己的应用代码中遇到相同的 ModuleNotFoundError,应检查子进程是否显式指定了正确的 Python 解释器;NamedTemporaryFile 的锁定问题仅在 Windows 出现,Linux/macOS 可能无法复现。
问题场景
运行 langchain-core 单元测试时触发该问题。具体涉及两个测试:test_importable_all_via_subprocess 在子进程中检查包的可导入性;test_from_file_encoding 验证 PromptTemplate.from_file 对 CP-1252/UTF-8 编码文件的解析。Issue 提供了在 libs/core 目录下执行 uv sync –all-groups –frozen 后将两者一并运行的复现脚本。
报错原文
test_importable_all_via_subprocess:
ModuleNotFoundError: No module named 'uuid_utils'
test_from_file_encoding:
PermissionError: [Errno 13] Permission denied: '<Windows temporary file>'
原因分析
存在两个独立的根因,均与 Windows 平台特性相关:
第一个报错的原因已确认:test_importable_all_via_subprocess 在子进程中调用裸 “python” 命令。Windows 通过 PATH 解析该命令时可能解析到基础解释器(base interpreter),而非当前激活的 uv 环境,导致子进程中缺少 uuid_utils 等依赖。
第二个报错的原因已确认:test_from_file_encoding 在 NamedTemporaryFile 仍处于打开状态时尝试重新打开该文件。Windows 对已打开的文件施加独占锁,任何重新打开操作都会触发 PermissionError,这是 Windows 的经典文件锁定行为。
环境排查
- 确认操作系统为 Windows(该问题在 POSIX 系统上无法复现)。
- 确认使用 uv 管理 Python 环境,并已执行 uv sync –all-groups –frozen 安装全部依赖。
- 确认 langchain-core 拉取的是当前 master 分支代码。
- 检查 pytest 版本,确保支持 tmp_path 夹具。
解决步骤
按 PR #39664 的修复方案执行(该方案已合并,且完整测试套件通过:2218 passed, 3 skipped, 9 xfailed, 2 xpassed):
- 在 test_importable_all_via_subprocess 中,将启动子进程的命令从 “python” 改为 sys.executable,确保子进程使用当前激活的 uv 管理解释器(即包含 uuid_utils 的环境)。
- 在 test_from_file_encoding 中,移除 NamedTemporaryFile 的使用,改用 pytest 的 tmp_path 夹具创建临时目录,配合 Path.write_text 写入测试文件,确保文件在 PromptTemplate.from_file 打开之前已关闭。
- 注意保留原有的 CP-1252/UTF-8 编码断言逻辑。
- 运行以下命令验证修改后的两个测试通过:
uv run pytest -q tests/unit_tests/test_imports.py::test_importable_all_via_subprocess tests/unit_tests/prompts/test_prompt.py::test_from_file_encoding - 如修改的是本地代码,建议进一步运行完整的 langchain-core 测试套件确认无回归。
验证方法
在 Windows 上重新运行上述两条测试命令,预期输出 2 passed。如需完整验证,可在 libs/core 目录下运行完整单元测试套件,预期结果与合并 PR 时一致(2218 passed)。
参考来源
langchain-ai/langchain #39647
修复 PR:langchain-ai/langchain #39664
AI 工具推荐
想把多个 AI 模型放在一个入口?
GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。
推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。
这个方案解决了吗?
可以继续搜索完整报错,或查看同一工具的其他排查指南。
![[Bug]: MTP spec decode still advances the grammar matcher after termination when a structural tag is built (residual after #44297)](https://www.chat-gpts.plus/wp-content/uploads/2026/08/52767-d83cfbd1-768x403.jpg)
![[Bug]: Native MTP speculative decoding degenerates into garbage token loops on deep agentic conversations (Qwen3-MoE)](https://www.chat-gpts.plus/wp-content/uploads/2026/08/47087-d5d5041b-768x403.jpg)
