【问题标题】:VS code - python - debug format issueVS 代码 - python - 调试格式问题
【发布时间】:2023-03-29 02:12:02
【问题描述】:

我正在尝试使用 VS 代码和 python 调试程序,但是当我尝试修改 json 以接受 arg 时,它给了我一个无效的语法问题。 我试图按照 MS 中的这个链接进行调试:https://code.visualstudio.com/docs/python/debugging 但我没有得到任何地方.. 我使用默认格式并添加了我的 arg:

{
    // 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: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "args": [
                "..\\4\\shopping\\shopping.csv"
            ],
            "console": "integratedTerminal"
        }
    ]
}

我尝试将其移动到不同的文件夹(在本例中为 \4),但我也尝试在工作区和购物文件夹中但没有成功 有人可以帮助我了解我哪里出错了吗? 谢谢

编辑 1:

添加更多细节:

这是完整的错误信息:

PS C:\Users\Carlo\source\repos\CS50AI\4\.vscode>  & 'python' 'c:\Users\Carlo\.vscode\extensions\ms-python.python-2020.11.371526539\pythonFiles\lib\python\debugpy\launcher' '50282' '--' 'c:\Users\Carlo\source\repos\CS50AI\4\.vscode\launch.json'
Traceback (most recent call last):
  File "C:\Users\Carlo\AppData\Local\Programs\Python\Python38-32\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\Carlo\AppData\Local\Programs\Python\Python38-32\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\Carlo\.vscode\extensions\ms-python.python-2020.11.371526539\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
    cli.main()
  File "c:\Users\Carlo\.vscode\extensions\ms-python.python-2020.11.371526539\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\Carlo\.vscode\extensions\ms-python.python-2020.11.371526539\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 "C:\Users\Carlo\AppData\Local\Programs\Python\Python38-32\lib\runpy.py", line 264, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
  File "C:\Users\Carlo\AppData\Local\Programs\Python\Python38-32\lib\runpy.py", line 239, in _get_code_from_file
    code = compile(f.read(), fname, 'exec')
  File "c:\Users\Carlo\source\repos\CS50AI\4\.vscode\launch.json", line 2
    // Use IntelliSense to learn about possible attributes.
    ^
SyntaxError: invalid syntax

不,我没有在 python 中获取 sys.argv

【问题讨论】:

  • 您在哪里遇到了无效的语法问题?当您打开 launch.json 时,您为 args 添加的行会突出显示?
  • 你在python脚本里面得到sys.argv中的参数吗
  • 编辑帖子以回答更多信息
  • 你使用的是什么版本的 VS Code? (帮助 > 关于)。我可以将您的 launch.json 与 1.51.1 版本一起使用
  • 1.51.1(用户设置)

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


【解决方案1】:

根据你提供的信息,我在终端重现了问题:

原因:在使用调试时,我们需要打开需要执行的脚本,而不是停留在“launch.json”文件中。

解决方法:打开要执行的python脚本并调试。

结果:

参考:Troubleshooting in VSCode

【讨论】:

  • 或者在launch.json中指定Python文件的路径 "program": "${workspaceFolder}/myfile.py",
【解决方案2】:

只需删除前三行 (cmets)。

{
"version": "0.2.0",
"configurations": [
    {
        "name": "Python: Current File",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "args": [
            "..\\4\\shopping\\shopping.csv"
        ],
        "console": "integratedTerminal"
    }
]}

【讨论】:

  • 不同的错误。没有看到 arg 发生异常: SystemExit Usage: python shopping.py data File "C:\Users\Carlo\source\repos\CS50AI\4\shopping\shopping.py", line 14, in main sys.exit( “用法:python shopping.py 数据”)文件“C:\Users\Carlo\source\repos\CS50AI\4\shopping\shopping.py”,第 184 行,在 main()
  • 能否提供 shopping.py 文件。我认为这会导致一些错误,因为这个 JSON 文件在我的 VS 代码上运行良好。
  • 我认为这是您要求的部分: import csv import sys from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier # 检查命令行参数 if len(sys.argv) ! = 2: sys.exit("用法:python shopping.py 数据")
猜你喜欢
  • 2018-11-19
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
  • 2021-03-26
  • 2019-05-08
  • 2019-05-14
相关资源
最近更新 更多