使用Emacs dap-mode调试Python时出现dap-server-path为nil错误
解决 dap-python 调试时的
wrong-type-argument stringp nil 错误 错误日志:
Debugger entered--Lisp error: (wrong-type-argument stringp nil) dap--create-session((:type "python" :cwd "/home/yelnat/Documents/programmin/python-test/" :request "launch" :name "My App" :program "/home/yelnat/Documents/programmin/python-test/test..." :args [] :dap-server-path (nil "-m" "debugpy.adapter"))) dap-start-debugging-noexpand((:type "python" :cwd "/home/yelnat/Documents/programmin/python-test/" :request "launch" :name "My App" :program "/home/yelnat/Documents/programmin/python-test/test..." :args [] :dap-server-path (nil "-m" "debugpy.adapter"))) #f(compiled-function () #<bytecode -0x785353ff795e90b>)() dap-debug((:type "python" :args "" :cwd "/home/yelnat/Documents/programmin/python-test/" :request "launch" :name "My App")) funcall-interactively(dap-debug (:type "python" :args "" :cwd "/home/yelnat/Documents/programmin/python-test/" :request "launch" :name "My App")) command-execute(dap-debug record) execute-extended-command(nil "dap-debug" "dap-deb") funcall-interactively(execute-extended-command nil "dap-debug" "dap-deb") command-execute(execute-extended-command)
问题原因:
从错误栈里的 dap-server-path (nil "-m" "debugpy.adapter") 可以直接定位问题:dap-mode 启动 debugpy 适配器时,第一个参数(Python 解释器路径)为空值 nil,导致类型匹配错误。本质是 dap-mode 没找到正确安装了 debugpy 的 Python 解释器。
修复步骤:
指定明确的 Python 解释器路径
在 Emacs 配置文件中添加以下代码,将路径替换为你实际的 Python 3.10 可执行文件路径(比如/usr/bin/python3.10):(setq dap-python-debugger 'debugpy) (setq dap-python-executable "/usr/bin/python3.10")验证目标 Python 环境中的 debugpy 安装
在终端运行以下命令,确认 debugpy 已安装在指定的 Python 环境中:/usr/bin/python3.10 -m pip list | grep debugpy如果没有输出结果,用这条命令重新安装:
/usr/bin/python3.10 -m pip install --user debugpy调整 Emacs 的 exec-path 配置
若使用了exec-path-from-shell,确保它没有过滤掉 Python 解释器所在的路径。也可以手动将路径添加到 Emacs 的exec-path:(add-to-list 'exec-path "/usr/bin")
注意事项:
Shell 中设置的 Python 别名对 Emacs 内部无效,必须指定完整的可执行文件路径,dap-mode 才能正确识别。
内容的提问来源于stack exchange,提问作者Yelnat
相关产品推荐
相关产品推荐

