在 vscode 中调试 pytest unittest 导致 AttributEerror sys has no attribute __breakpointhook__ on exit

Debugging pytest unitest in vscode results in AttributError sys has no attribute __breakpointhook__ on exit

我在 Ubuntu 18.04 和 Anaconda 版本 4.5.10 上使用 visual studio 代码 1.27.2 来管理我的虚拟环境。

我在 vscode 中安装了 Anaconda 和 python 扩展。

我使用虚拟环境进行了简单测试,其中我只为 python 3.6 安装了 pytest 和 pylint。

如果我运行这个简单的脚本:

import pytest

def test_test():
    assert True == True

Visual Studio 代码 运行 测试完全正常并在输出控制台中显示结果。

如果我尝试调试此脚本,但是,测试 运行s 并在退出时,我得到一个 AttributeError sys has no attribute __breakpointhook__ 我不明白为什么?难道我做错了什么?这是 Visual Studio 代码调试器或 pytest 集成的错误吗?

堆栈跟踪:

File "/home/leerro/anaconda3/envs/py36TF/lib/python3.6/runpy.py", line 193, in _run_module_as_main "__main__", mod_spec)
File "/home/leerro/anaconda3/envs/py36TF/lib/python3.6/runpy.py", line 85, in _run_code exec(code, run_globals)
File "/home/leerro/.vscode/extensions/ms-python.python-2018.8.0/pythonFiles/experimental/ptvsd/ptvsd/__main__.py", line 211, in <module> singlesession=args.single_session)
File "/home/leerro/.vscode/extensions/ms-python.python-2018.8.0/pythonFiles/experimental/ptvsd/ptvsd/__main__.py", line 205, in main debug_main(addr, name, kind, *extra, **kwargs)
File "/home/leerro/.vscode/extensions/ms-python.python-2018.8.0/pythonFiles/experimental/ptvsd/ptvsd/_local.py", line 28, in debug_main run_module(address, name, *extra, **kwargs)
File "/home/leerro/.vscode/extensions/ms-python.python-2018.8.0/pythonFiles/experimental/ptvsd/ptvsd/_local.py", line 53, in run_module run(argv, addr, **kwargs)
File "/home/leerro/.vscode/extensions/ms-python.python-2018.8.0/pythonFiles/experimental/ptvsd/ptvsd/_local.py", line 125, in _run _pydevd.main()
File "/home/leerro/.vscode/extensions/ms-python.python-2018.8.0/pythonFiles/experimental/ptvsd/ptvsd/_vendored/pydevd/pydevd.py", line 1743, in main debugger.connect(host, port)
File "/home/leerro/.vscode/extensions/ms-python.python-2018.8.0/pythonFiles/experimental/ptvsd/ptvsd/_vendored/pydevd/pydevd.py", line 1099, in run return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "/home/leerro/.vscode/extensions/ms-python.python-2018.8.0/pythonFiles/experimental/ptvsd/ptvsd/_vendored/pydevd/pydevd.py", line 1123, in _exec runpy._run_module_as_main(module_name, alter_argv=False)
File "/home/leerro/anaconda3/envs/py36TF/lib/python3.6/runpy.py", line 193, in _run_module_as_main "__main__", mod_spec)
File "/home/leerro/anaconda3/envs/py36TF/lib/python3.6/runpy.py", line 85, in _run_code exec(code, run_globals)
File "/home/leerro/anaconda3/envs/py36TF/lib/python3.6/site-packages/pytest.py", line 77, in <module> raise SystemExit(pytest.main())
File "/home/leerro/anaconda3/envs/py36TF/lib/python3.6/site-packages/_pytest/config/__init__.py", line 67, in main return config.hook.pytest_cmdline_main(config=config)
File "/home/leerro/anaconda3/envs/py36TF/lib/python3.6/site-packages/pluggy/hooks.py", line 258, in __call__ return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
File "/home/leerro/anaconda3/envs/py36TF/lib/python3.6/site-packages/pluggy/manager.py", line 67, in _hookexec return self._inner_hookexec(hook, methods, kwargs)
File "/home/leerro/anaconda3/envs/py36TF/lib/python3.6/site-packages/pluggy/manager.py", line 61, in <lambda> firstresult=hook.spec_opts.get('firstresult'),
File "/home/leerro/anaconda3/envs/py36TF/lib/python3.6/site-packages/pluggy/callers.py", line 201, in _multicall return outcome.get_result()
File "/home/leerro/anaconda3/envs/py36TF/lib/python3.6/site-packages/pluggy/callers.py", line 76, in get_result raise ex[1].with_traceback(ex[2])
File "/home/leerro/anaconda3/envs/py36TF/lib/python3.6/site-packages/pluggy/callers.py", line 180, in _multicall res = hook_impl.function(*args)
File "/home/leerro/anaconda3/envs/py36TF/lib/python3.6/site-packages/_pytest/main.py", line 208, in pytest_cmdline_main return wrap_session(config, _main)
File "/home/leerro/anaconda3/envs/py36TF/lib/python3.6/site-packages/_pytest/main.py", line 203, in wrap_session config._ensure_unconfigure()
File "/home/leerro/anaconda3/envs/py36TF/lib/python3.6/site-packages/_pytest/config/__init__.py", line 611, in _ensure_unconfigure fin()
File "/home/leerro/anaconda3/envs/py36TF/lib/python3.6/site-packages/_pytest/debugging.py", line 67, in fin sys.breakpointhook = sys.__breakpointhook__
AttributeError: module 'sys' has no attribute '__breakpointhook__'

工作区设置:

{
    "python.unitTest.pyTestEnabled": true,
    "python.unitTest.unittestEnabled": false,
    "python.unitTest.nosetestsEnabled": false,
    "python.unitTest.autoTestDiscoverOnSaveEnabled": true,
    "python.pythonPath": "/home/gabriel/anaconda3/envs/pytestenv/bin/python"
}

我在 launch.json 中使用此配置尝试 运行 从调试会话中启用它,但它给了我相同的堆栈跟踪和错误。

{
     "name": "Debug Pytest",
     "type": "python",
     "request": "launch",
     "stopOnEntry": false,
     "pythonPath": "${config:python.pythonPath}",
     "module": "pytest",
     "envFile": "${workspaceFolder}/.env",
     "console": "integratedTerminal",
}

我有点迷路了,不知道下一步该去哪里搜索。当然,我仍然可以调试我的代码,因为只有在退出时才会发生异常,但我发现它很麻烦,我不知道这是 Visual Studio 代码还是我的配置的问题。

如有任何帮助,我们将不胜感激!

它是一个 bug in ptvsd,将在其下一个版本中修复,它将包含在下一个版本的 VS Code Python 扩展中。