【问题标题】:VSCode Python DebuggerVSCode Python 调试器
【发布时间】:2020-11-08 03:04:09
【问题描述】:

我正在尝试调试一个简单的 python 文件。 test.py 文件只包含文本 print("hello")。 当我选择 Run->Start Debugging 时,我会看到一个带有以下文本的模式对话框:

h.toLowerCase 不是函数

当我打开配置时,我看到:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "module": "simple"
        }
    ]
}

这是导航到帮助->关于->复制的结果。

版本:1.51.0(用户设置)提交: fcac248b077b55bae4ba5bab613fd6e9156c2f0c 日期: 2020-11-05T18:18:23.642Z 电子:9.3.3 铬:83.0.4103.122 Node.js:12.14.1 V8:8.3.110.13-electron.0 操作系统:Windows_NT x64 10.0.19042

如果我启动一个命令窗口,我可以输入 python 或 pip,所以这些东西在我的路径中。

Python --version generates
3.9.0

VS Code 扩展显示我安装了以下扩展。

Python v2020.10.332292344 用于 VSCode 0.2.3 的 Python

如果我右键单击 python 文件并单击在控制台中运行 python。它工作正常。我在控制台中看到文本“Hello”。

【问题讨论】:

  • 你执行过命令吗:Python: Select Interpreter
  • 你能分解一下我是怎么做的吗?在VSC的左下角,它说Python 3.9.0 64 bit error 0 warning 0 Python Current File (t2)
  • 右下角选择语言模式是说python。
  • 为什么python解释器会调用javascript函数
  • 我已经尝试卸载并重新安装 VSCode 和 Python 对这个盒子没有影响。

标签: python visual-studio-code


【解决方案1】:
{
    "name": "Python",
    "type": "python",
    "pythonPath":"${config.python.pythonPath}", 
    "request": "launch",
    "stopOnEntry": true,
    "console": "none",
    "program": "${file}",
    "cwd": "${workspaceRoot}",
    "debugOptions": [
        "WaitOnAbnormalExit",
        "WaitOnNormalExit",
        "RedirectOutput"
    ],
    "env": {"name":"value"}
}

【讨论】:

  • 我希望我能分享一个截图。我将此 json 作为我的配置添加到了 launch.json 文件中。 python路径变量加下划线,debugOptions加下划线。
【解决方案2】:

好的,想通了。这篇文章很有帮助。

https://code.visualstudio.com/docs/python/debugging

有一个称为控制台的可选键。可能的值:internalConsole、externalTerminal 或默认的集成终端。暂时将此键设置为 externalTerminal。再次运行 F5 或 Run->Start Debug。您将收到如下所示的错误消息。

Traceback (most recent call last):
  File "...\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "...\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "...\.vscode\extensions\ms-python.python-2020.10.332292344\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
    cli.main()
  File "...\.vscode\extensions\ms-python.python-2020.10.332292344\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "...\.vscode\extensions\ms-python.python-2020.10.332292344\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 267, in run_file
    runpy.run_path(options.target, run_name=compat.force_str("__main__"))
  File "...\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 267, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
  File "...\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 242, in _get_code_from_file
    code = compile(f.read(), fname, 'exec')
  File "...\python\.vscode\launch.json", line 2
    // Use IntelliSense to learn about possible attributes.
    ^
SyntaxError: invalid syntax
Press any key to continue . . .

错误消息指出它卡在 Intellisense 评论上。作为开发人员,cmets 通常是个好东西。但这显然是模板化的 launch.json 文件中的一个问题。如果您删除 cmets 并将控制台键设置为 internalConsole,一切似乎都可以正常工作。

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "module": "simple",
            "console": "internalConsole"
        }
    ]
}

【讨论】:

  • 我的 launch.json 有 cmets 并且我在调试 python 文件时没有问题,如果我在 launch.json 是我的当前文件时按 F5 会收到此错误/异常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-24
  • 2018-12-02
  • 2020-04-28
  • 1970-01-01
  • 1970-01-01
  • 2021-10-09
  • 2021-12-06
相关资源
最近更新 更多