Nested subprocess `python -m ...` fails module resolution only when parent is launched via debugpy
还没有人认领这个 Issue。
评估
调研方向
首先使用 VS Code 的 launch.json 形态以及 python -m debugpy --listen ... -m ... 重现该故障,然后将嵌套 subprocess.run 的环境和模块解析与非调试启动进行比较。完成的标准是确定是否由 debugpy 导致不同的 -m 解析,并记录或实现一个确定性的修复,同时涵盖 Windows 的 src/ 布局和 PYTHONPATH 情况。
由索引模型根据 Issue 内容生成。
描述
Summary
In a Windows project with src/ layout, a parent Python module runs correctly under debugpy, but a child subprocess started from that parent with:
python -m my_tools.tasks.validation.main
fails with:
No module named my_tools.tasks.validation.main
The exact same child command succeeds outside debugpy (normal terminal/task run).
Environment
- OS: Windows (PowerShell)
- Python: virtualenv interpreter (
.venv\Scripts\python.exe), debugpy 1.8.19 - VS Code Python extension (debugpy launcher used by VS Code)
- Project layout:
src/my_tools/... PYTHONPATHin launch config:<workspace>/src
Expected behavior
If parent process is launched via debugpy and parent starts child with:
<venv_python> -m my_tools.tasks.validation.main ...
then child should resolve and execute module exactly as in terminal mode.
Actual behavior
When parent is launched via debugpy (module launch in launch.json), child subprocess fails:
No module named my_tools.tasks.validation.main
When parent is launched normally (no debugpy), same child command succeeds.
Reproduction pattern
Parent debug launch (fails in child)
VS Code launch command shape:
<venv>\python.exe <...>\debugpy\launcher <port> -- -m my_tools.service.runner.main ...
Inside parent:
cmd = [
sys.executable,
"-m",
"my_tools.tasks.validation.main",
"--help",
]
subprocess.run(
cmd,
cwd="<workspace>",
env=env_with_pythonpath,
capture_output=True,
text=True,
)
Result under debug launch:
stderr: No module named my_tools.tasks.validation.main
Parent non-debug launch (works)
python -m my_tools.service.runner.main ...
Result: child module resolves and completes successfully.
Manual debugpy CLI launch (also fails)
After installing debugpy into the same venv, launching parent via CLI debugpy reproduces the same child failure:
python -m debugpy --listen 5678 -m my_tools.service.runner.main --queue-root <...> --work-root <...> --once --log-level DEBUG
Result:
stderr: No module named my_tools.tasks.validation.main
Sanity checks already done
All of the following succeed in same venv:
python -c "import os,sys,importlib.util as u,my_tools; print(sys.executable); print(os.getenv('PYTHONPATH')); print(u.find_spec('my_tools.tasks.validation.main'))"
find_spec(...) returns valid ModuleSpec under <workspace>/src/my_tools/....
Also succeeds:
python -c "import os,sys,subprocess; env=os.environ.copy(); env['PYTHONPATH']=r'<workspace>\\src;<workspace>/src'; cmd=[sys.executable,'-m','my_tools.tasks.validation.main','--help']; r=subprocess.run(cmd,cwd=r'<workspace>',env=env,capture_output=True,text=True); print(r.returncode); print(r.stderr)"
Return code is 0, stderr empty.
Why this looks debugpy-related
- Same interpreter path.
- Same module path.
- Same child command works outside debugpy.
- Failure appears when parent is launched via debugpy (VS Code launcher and manual
python -m debugpy). - The failing point is nested child module resolution via
-m.
Current workaround
- Run parent as normal task/terminal (non-debug) for production-like execution.
- Alternative code workaround: launch child by script path instead of
-m, e.g.:
python <workspace>/src/my_tools/tasks/validation/main.py ...
Question to maintainers
Is this a known issue/limitation with:
- debugpy launcher + nested subprocess
-mcalls src/layout +PYTHONPATH- Windows-specific env/path propagation
and is there a recommended debugpy setting to make nested child -m module resolution deterministic?
- 主要语言
- Python
- 星标
- 2.5k
- 派生
- 202
- 平均合并
- 5 天 1 小时
- 30 天内合并 PR
- 1
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
microsoft/debugpy 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
-
documentation
难度 2/5 1-3 小时 新手友好度 68/100
-
难度 2/5 1-3 小时 新手友好度 78/100
-
documentation
难度 2/5 1-3 小时 新手友好度 62/100
-
难度 3/5 1-2 天 新手友好度 68/100
查看 microsoft/debugpy 的全部 Issue
相似的 Issue
-
bug confirmed issue
难度 2/5 1-3 小时 新手友好度 75/100
open-webui/open-webui#30750 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
-
enhancement
难度 2/5 1-3 小时 新手友好度 75/100
OpenwaterHealth/openmotion-bloodflow-app#604 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 70/100
-
good first issue
难度 1/5 1 小时以内 新手友好度 90/100