【问题标题】:Using VS Code to debug python files. Exception thrown on breakpoint and breakpoint is ignored使用 VS Code 调试 python 文件。断点抛出异常,断点被忽略
【发布时间】:2020-11-28 02:40:50
【问题描述】:

尝试了多个不同的 python 文件。每次我尝试在 vs 代码中使用调试器并设置断点时,都会忽略断点并引发异常并且脚本继续运行。我一直在谷歌搜索和修补超过 2 小时,似乎无法弄清楚这里发生了什么。尝试重新启动PC,以管理员身份运行vs code,卸载/重新安装vs code的python扩展。试图挖掘回溯中提到的文件并查明似乎引发异常的函数,但我无法弄清楚它是从哪里调用的,或者它为什么引发异常。我对 Python 还是很陌生。在我的笔记本电脑上调试工作正常,但无论出于何种原因,我的桌面出现了这个问题。

Traceback (most recent call last):
  File "c:\Users\Joel\.vscode\extensions\ms-python.python-2020.7.96456\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 529, in _original_file_to_client
    return cache[filename]
KeyError: 'c:\\users\\joel\\local settings\\application data\\programs\\python\\python37-32\\lib\\runpy.py'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\Joel\.vscode\extensions\ms-python.python-2020.7.96456\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 330, in _on_run
    self.process_net_command_json(self.py_db, json_contents)
  File "c:\Users\Joel\.vscode\extensions\ms-python.python-2020.7.96456\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_process_net_command_json.py", line 190, in process_net_command_json    
    cmd = on_request(py_db, request)
  File "c:\Users\Joel\.vscode\extensions\ms-python.python-2020.7.96456\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_process_net_command_json.py", line 771, in on_stacktrace_request       
    self.api.request_stack(py_db, request.seq, thread_id, fmt=fmt, start_frame=start_frame, levels=levels)
  File "c:\Users\Joel\.vscode\extensions\ms-python.python-2020.7.96456\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_api.py", line 214, in request_stack
    if internal_get_thread_stack.can_be_executed_by(get_current_thread_id(threading.current_thread())):
  File "c:\Users\Joel\.vscode\extensions\ms-python.python-2020.7.96456\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 661, in can_be_executed_by
    py_db, self.seq, self.thread_id, frame, self._fmt, must_be_suspended=not timed_out, start_frame=self._start_frame, levels=self._levels)
  File "c:\Users\Joel\.vscode\extensions\ms-python.python-2020.7.96456\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_net_command_factory_json.py", line 213, in make_get_thread_stack_message
    py_db, frames_list
  File "c:\Users\Joel\.vscode\extensions\ms-python.python-2020.7.96456\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_net_command_factory_xml.py", line 175, in _iter_visible_frames_info    
    new_filename_in_utf8, applied_mapping = pydevd_file_utils.norm_file_to_client(filename_in_utf8)
  File "c:\Users\Joel\.vscode\extensions\ms-python.python-2020.7.96456\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 531, in _original_file_to_client
    translated = _path_to_expected_str(get_path_with_real_case(_AbsFile(filename)))
  File "c:\Users\Joel\.vscode\extensions\ms-python.python-2020.7.96456\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\Joel\.vscode\extensions\ms-python.python-2020.7.96456\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd_file_utils.py", line 184, in _resolve_listing
    dir_contents = cache[resolved_lower] = os.listdir(resolved)
PermissionError: [WinError 5] Access is denied: 'C:\\Users\\Joel\\Local Settings' 

所以每次遇到断点时我都会得到这个回溯。看一下“pydevd_file_utils.py”中的“_original_file_to_client”函数,我们得到:

def _original_file_to_client(filename, cache={}):
    try:
        return cache[filename]
    except KeyError:
        translated = _path_to_expected_str(get_path_with_real_case(_AbsFile(filename)))
        cache[filename] = (translated, False)
    return cache[filename]

我无法弄清楚这个函数是从哪里调用的,或者预期的输出应该是什么。对此的任何帮助将不胜感激!

编辑:如果从跟踪中看不出来,忘记提及我使用的是 Windows 10

【问题讨论】:

  • 您是否尝试取消选中“断点”下的“引发异常”和“未捕获异常”?唯一的断点是您在脚本中设置的。
  • @liamsuma 刚试过。仍然遇到同样的问题。

标签: python python-3.x debugging visual-studio-code


【解决方案1】:

This 是一个类似的问题。文件名中的空格会导致此问题: “本地设置”、“应用程序数据”

【讨论】:

  • 我认为这也可能是问题,但是当我去检查它时,我的用户文件夹中什至没有“本地设置”文件夹。这不是一个有效的目录。
  • 您的解释器似乎有一些问题。可以选择虚拟环境还是重新安装全局python?
猜你喜欢
  • 2017-07-20
  • 1970-01-01
  • 2018-02-20
  • 1970-01-01
  • 2014-07-17
  • 1970-01-01
  • 2018-03-08
  • 2014-04-03
  • 1970-01-01
相关资源
最近更新 更多