首页 > 解决方案 > 使用 Pythonq 在 VSCode 中传递“args”时语法无效:“...”

问题描述

将命令行参数传递给我正在调试的 python 脚本时,我遵循以下指示

https://github.com/Microsoft/vscode/issues/28059

通常我会使用不需要dir前缀的 dir 参数启动脚本

例如:python script.py c:\output_folder启动脚本并设置c:\output_folder为 dir 变量

所以按照网上的指示,我在launch.json文件中有以下内容

{
    // 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}",
            "preLaunchTask": "shellCommand",
            "console": "integratedTerminal",
            "args": ["c:\\output_folder"]
        }
    ]
}

尝试在 VS Code 中调试 python 文件时,出现此错误:

Traceback (most recent call last):
  File "C:\python\portablepython\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\python\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "c:\Users\Admin\.vscode\extensions\ms-python.python-2020.3.71659\pythonFiles\lib\python\debugpy\wheels\debugpy\__main__.py", line 45, in <module>
    cli.main()
  File "c:\Users\Admin\.vscode\extensions\ms-python.python-2020.3.71659\pythonFiles\lib\python\debugpy\wheels\debugpy/..\debugpy\server\cli.py", line 429, in main
    run()
  File "c:\Users\Admin\.vscode\extensions\ms-python.python-2020.3.71659\pythonFiles\lib\python\debugpy\wheels\debugpy/..\debugpy\server\cli.py", line 266, in run_file
    runpy.run_path(options.target, run_name=compat.force_str("__main__"))
  File "C:\python\lib\runpy.py", line 261, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
  File "C:\python\lib\runpy.py", line 236, in _get_code_from_file
    code = compile(f.read(), fname, 'exec')
  File "C:\code\.vscode\launch.json", line 2
    // Use IntelliSense to learn about possible attributes.
     ^
SyntaxError: invalid syntax

它显然给出了一个错误,launch.json所以我知道它不在 python 脚本中

为什么"args"线路不工作launch.json

谢谢

标签: pythonvisual-studio-code

解决方案


因为您已"program": "${file}"设置它意味着调试器将启动您当前打开的文件。根据回溯,您似乎正在以launch.jsonopen 作为当前文件启动调试器。切换到您要调试的文件,它应该可以正常工作。


推荐阅读