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



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

我在这个VSCode项目中的调试配置,一个Discord机器人,在我开始调试时突然出现了错误。它在正常运行程序时不会这样做,在其他项目中调试似乎也可以。

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

这是错误文本,我为转储这么多代码道歉,但我不知道这是否有意义:

'c:UsersLucas.vscodeextensionsms-python.python-2021.5.829140558pythonFileslibpythondebugpylauncher' '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:UsersLucas.vscodeextensionsms-python.python-2021.5.829140558pythonFileslibpythondebugpy_vendoredpydevdpydevd_file_utils.py", line 221, in _get_path_with_real_case
return _resolve_listing(drive, iter(parts))
File "c:UsersLucas.vscodeextensionsms-python.python-2021.5.829140558pythonFileslibpythondebugpy_vendoredpydevdpydevd_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:UsersLucas.vscodeextensionsms-python.python-2021.5.829140558pythonFileslibpythondebugpy_vendoredpydevdpydevd_file_utils.py", line 226, in _get_path_with_real_case
return _resolve_listing(drive, iter(parts))
File "c:UsersLucas.vscodeextensionsms-python.python-2021.5.829140558pythonFileslibpythondebugpy_vendoredpydevdpydevd_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:UsersLucas.vscodeextensionsms-python.python-2021.5.829140558pythonFileslibpythondebugpy_vendoredpydevdpydevd_file_utils.py", line 221, in _get_path_with_real_case
return _resolve_listing(drive, iter(parts))
File "c:UsersLucas.vscodeextensionsms-python.python-2021.5.829140558pythonFileslibpythondebugpy_vendoredpydevdpydevd_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:UsersLucas.vscodeextensionsms-python.python-2021.5.829140558pythonFileslibpythondebugpy_vendoredpydevdpydevd_file_utils.py", line 226, in _get_path_with_real_case
return _resolve_listing(drive, iter(parts))
File "c:UsersLucas.vscodeextensionsms-python.python-2021.5.829140558pythonFileslibpythondebugpy_vendoredpydevdpydevd_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是什么,也不知道是否有办法重新安装它并解决问题。

有什么变通办法吗?我真的不知道我在问什么,纯粹是因为这对我来说太陌生了,但这只是那些似乎毫无真正原因地自发发生的错误之一。

答案:

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

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

背景:

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

您使用的file name(VS Code称之为program(是bot,此语法不再有效。同样工作的./bot将不再工作,但会抛出一个略有不同的错误。我不确定这个更改是一个bug还是一个预期的更改。

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

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

这假设您从工作区的根目录启动工作区。当您打开集成终端时,您可以通过启动目录来判断这一点。

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

发生此错误的原因是python调试器从当前打开的文件夹执行终端中的文件(默认情况下(
但您可以更改此行为,在文件的目录中使用execute:

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

当您知道文件位于正确的位置并且指向正确的方向时,这将消除FileNotFoundError。

最新更新