VSCode 由于 PyDev FileNotFoundError,调试模式有时不起作用

VSCode Debug Mode sometimes does not work due to a PyDev FileNotFoundError

老实说,我不知道我在看什么。

我在这个 VSCode 项目中的调试配置,一个 Discord 机器人,在我开始调试时突然出现错误。 运行程序正常的时候不会这样,在其他项目调试好像没问题。

大约 50% 的时间,尽管有错误并且调试工作正常,它最终会连接到 Discord,但其他时候程序会挂起并拒绝连接到 Discord。

这是错误文本,对于转储了这么多代码,我深表歉意,但我不知道这是否重要:

'c:\Users\Lucas\.vscode\extensions\ms-python.python-2021.5.829140558\pythonFiles\lib\python\debugpy\launcher' '51717' '--' 'bot/bot.py' 
pydev debugger: critical: unable to get real case for file. Details:
filename: bot
drive:
parts: ['bot']
(please create a ticket in the tracker to address this).
Traceback (most recent call last):
  File "c:\Users\Lucas\.vscode\extensions\ms-python.python-2021.5.829140558\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 221, in _get_path_with_real_case
    return _resolve_listing(drive, iter(parts))
  File "c:\Users\Lucas\.vscode\extensions\ms-python.python-2021.5.829140558\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 184, in _resolve_listing
    dir_contents = cache[resolved_lower] = os.listdir(resolved)
FileNotFoundError: [WinError 3] The system cannot find the path specified: ''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\Lucas\.vscode\extensions\ms-python.python-2021.5.829140558\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 226, in _get_path_with_real_case
    return _resolve_listing(drive, iter(parts))
  File "c:\Users\Lucas\.vscode\extensions\ms-python.python-2021.5.829140558\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 184, in _resolve_listing
    dir_contents = cache[resolved_lower] = os.listdir(resolved)
FileNotFoundError: [WinError 3] The system cannot find the path specified: ''
pydev debugger: critical: unable to get real case for file. Details:
filename: bot
drive:
parts: ['bot']
(please create a ticket in the tracker to address this).
Traceback (most recent call last):
  File "c:\Users\Lucas\.vscode\extensions\ms-python.python-2021.5.829140558\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 221, in _get_path_with_real_case
    return _resolve_listing(drive, iter(parts))
  File "c:\Users\Lucas\.vscode\extensions\ms-python.python-2021.5.829140558\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 184, in _resolve_listing
    dir_contents = cache[resolved_lower] = os.listdir(resolved)
FileNotFoundError: [WinError 3] The system cannot find the path specified: ''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\Lucas\.vscode\extensions\ms-python.python-2021.5.829140558\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 226, in _get_path_with_real_case
    return _resolve_listing(drive, iter(parts))
  File "c:\Users\Lucas\.vscode\extensions\ms-python.python-2021.5.829140558\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 184, in _resolve_listing
    dir_contents = cache[resolved_lower] = os.listdir(resolved)
FileNotFoundError: [WinError 3] The system cannot find the path specified: ''

我已经针对错误中提到的 tracker 发布了一张票,但老实说,我不知道 PyDev 是什么,或者是否有办法重新安装它并解决问题。

有什么解决方法吗?我真的不知道我在问什么,纯粹是因为这对我来说太陌生了,但这只是那些似乎没有任何真正原因而自发发生的错误之一。

答案:

"program": "bot" 更改为 ./.vscode/launch.json 中的 "program": "${workspaceFolder}\bot.py"

对于大多数人来说,文件名不会是 bot,而是 mainscript 或任何您命名的 Python 脚本文件。所以你会使用 "${workspaceFolder}\main.py"

背景:

在 pydev 调试器的最新更新中,我认为他们已经更新了文件位置的解析方式。

您使用的 file name(VS Code 称之为 program)是 bot,此语法不再有效。./bot 也曾经有效不再有效,但会抛出一个稍微不同的错误。我不确定此更改是错误还是有意更改。

您的 Python VS Code 工作区的调试配置位于 {workspaceFolder}/.vscode/launch.json 下(如果您还没有 launch.json,请创建一个)。

您必须使用 {workspaceFolder}\ 以及正确的文件名和位置将 "program": "bot" 更改为 "program": "${workspaceFolder}\bot.py"

这假定您从其根目录启动您的工作区。你可以通过打开集成终端时的起始目录来判断。

这是出现此类错误的原因以及更好的修复方法

The error occurs because the python debugger executes files in the terminal from the current open folder (by default)
But you can change this behavior, to use execute in the file's directory:

  • 转到vscode设置(或使用快捷键:ctrl+逗号
  • 然后在设置
  • 中搜索这个@ext:ms-python.python execute
  • 您会看到设置“在文件目录中执行”
  • 然后选中复选框,在文件目录而不是当前打开的文件夹中执行代码
  • 返回您的代码并重新运行(即单击调试器 运行 按钮)

This eliminates the FileNotFoundError when you know your file is sitting in the right place, and you are pointing in the right direction.