如何配置debugpy以调试通过pip install -e安装的Python包?
解决Editable安装的Python包调试导入问题
问题原因
尽管通过pip install -e .完成了可编辑安装,但调试器运行时的环境可能未正确识别包路径,或是当前工作目录设置导致Python无法找到mypackage。
解决方案
1. 先确认可编辑安装状态
执行pip show mypackage,查看Location字段是否指向项目的src/mypackage目录。若路径不符,重新执行pip install -e .确保安装生效。
2. 修改debugpy配置
以下三种方案任选其一:
方案一:添加PYTHONPATH环境变量
在配置的env中加入项目src目录路径,让Python能定位到包:
{ "request": "launch", "type": "python", "cwd": "${fileDirname}", "program": "${file}", "stopOnEntry": true, "console": "integratedTerminal", "env": { "PYDEVD_RESOLVE_SYMLINKS": "1", "PYTHONPATH": "${workspaceFolder}/src" } }
方案二:指定正确的Python解释器
确保调试使用的解释器与安装可编辑包的环境一致(比如虚拟环境的Python):
{ "request": "launch", "type": "python", "cwd": "${fileDirname}", "program": "${file}", "stopOnEntry": true, "console": "integratedTerminal", "pythonPath": "${workspaceFolder}/venv/bin/python", // Windows系统替换为 "${workspaceFolder}/venv/Scripts/python.exe" "env": { "PYDEVD_RESOLVE_SYMLINKS": "1" } }
方案三:调整工作目录到项目根
将cwd设置为项目根目录,让Python识别可编辑安装的包路径:
{ "request": "launch", "type": "python", "cwd": "${workspaceFolder}", "program": "${file}", "stopOnEntry": true, "console": "integratedTerminal", "env": { "PYDEVD_RESOLVE_SYMLINKS": "1" } }
额外检查点
- 确认调试会话激活了正确的虚拟环境(若使用)
- 检查项目的
pyproject.toml/setup.py/setup.cfg配置,确保包路径设置正确(比如pyproject.toml中[tool.setuptools.packages.find] where = ["src"])
内容的提问来源于stack exchange,提问作者Barzi2001
相关产品推荐
相关产品推荐

