【问题标题】:Configure Vs code version 2.0.0 Build Task for python为python配置vs代码2.0.0版构建任务
【发布时间】:2017-07-31 07:31:06
【问题描述】:

我需要帮助来配置我的 Vs 代码以使用 Cntrl Shift B 在 python 中运行脚本,在 Vs 代码升级到版本 2.0.0 之前我工作正常,现在它要我配置 Build。而且我完全不知道 Build 是关于什么的。

过去,当我只需要配置任务运行器时,它运行良好。有任务运行器的 youtube 视频。我似乎无法理解 Build 的全部内容。

【问题讨论】:

    标签: python visual-studio-code vscode-settings


    【解决方案1】:

    在 VS Code 中转到任务 -> 配置任务

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            {
                "taskName": "Run File",
                "command": "python ${file}",
                "type": "shell",
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "presentation": {
                    "reveal": "always",
                    "panel": "new",
                    "focus": true
                }
            },
            {
                "taskName": "nosetest",
                "command": "nosetests -v",
                "type": "shell",
                "group": {
                    "kind": "test",
                    "isDefault": true
                },
                "presentation": {
                    "reveal": "always",
                    "panel": "new",
                    "focus": true
                }
            }
        ]
    }
    

    command: 运行当前 python 文件

    group: '构建'

    presentation:

    • 运行时始终显示 shell
    • 使用新的外壳
    • 聚焦外壳(即在外壳窗口中捕获键盘)

    第二个任务被配置为默认测试,并在当前在 VS Code 中打开的文件夹中运行 nosetest -v

    “运行构建任务”(绑定到Ctrl+Shift+B)是配置为默认构建任务的任务,在此示例中为任务 1(请参阅group 条目)。

    编辑:
    由 cmets 中的@RafaelZayas 建议(这将使用 VS Code 设置中指定的 Python 解释器,而不是系统默认值;有关更多信息,请参阅他的评论):

    "command": "${config:python.pythonPath} ${file}"

    【讨论】:

    • 我试过你的代码,但它显示错误> Executing task: python d:\books\programming Languages\python\Projects\.vscode\tasks.json < C:\Users\[myuser]\Anaconda3\python.exe: can't open file 'd:\books\programming': [Errno 2] No such file or directory The terminal process terminated with exit code: 1
    • 我可以推荐:"command": "${config:python.pythonPath} ${file}"` 用 config 变量替换 python 将使用与 python VS Code 中设置的相同的 pythonPath延期。如果在 Windows 上,这将使用 cmd.exe 或 PowerShell 运行。根据哪个(我认为 PowerShell 适用于 Win 10 或更高版本),在 $file 变量周围加上单引号或双引号,即 "command":"${config:python.pythonPath} '${file}'"跨度>
    • 很棒的配置,魅力十足。只是想补充一点,他们最近将 taskName 更改为 label,这不是什么大问题,只是 VS Code 中的警告。
    【解决方案2】:

    ...没有足够的声誉来评论已接受的答案...

    至少在我的环境(Ubuntu 18.04,w/virtual env)中,如果使用“args”传入参数,文件必须是第一个参数,就像@VladBezden 所做的那样,并且不是@orangeInk 正在执行的命令的一部分。否则我会收到消息“没有这样的文件或目录”。

    具体来说,@VladBezden 的答案确实对我有用,而以下

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            {
                "label": "build",
                "command": "${config:python.pythonPath} setup.py", // WRONG, move setup.py to args
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "args": [
                    "install"
                ],
                "presentation": {
                    "echo": true,
                    "panel": "shared",
                    "focus": true
                }
            }
        ]
    }
    

    这花了我一段时间才弄清楚,所以我想我会分享。

    【讨论】:

      【解决方案3】:

      这是我的构建配置 (Ctrl+Shift+B)

      tasks.json

      {
          // See https://go.microsoft.com/fwlink/?LinkId=733558
          // for the documentation about the tasks.json format
          "version": "2.0.0",
          "tasks": [
              {
                  "label": "build",
                  "command": "python",
                  "group": {
                      "kind": "build",
                      "isDefault": true
                  },
                  "args": [
                      "setup.py",
                      "install"
                  ],
                  "presentation": {
                      "echo": true,
                      "panel": "shared",
                      "focus": true
                  }
              }
          ]
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-14
        • 2015-08-01
        • 2018-01-21
        • 2021-06-07
        • 1970-01-01
        • 2011-08-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多