为Python配置Vs code版本2.0.0构建任务

6

我需要帮助配置我的Vs代码,使用Cntrl Shift B运行Python脚本,之前一切正常,直到Vs代码升级到2.0.0版本后,现在它要求我配置构建。我对构建一无所知。

过去当我只需要配置任务运行器时就可以很好地工作了。有关任务运行器的YouTube视频。我似乎无法理解构建的含义。

3个回答

21

在VS Code中,进入Tasks -> Configure Tasks。

{
    // 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: 'build'

presentation:

  • 始终显示 shell 窗口
  • 使用新的 shell
  • 聚焦于 shell 窗口(即键盘输入会被捕获到 shell 窗口中)

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

"Run Build Task"(用于绑定快捷键 Ctrl+Shift+B 的任务)是配置为默认构建任务的任务 1(参见 group 条目)。

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

"command": "${command:python.interpreterPath} ${file}"


我尝试了你的代码,但是它显示了错误 `> 执行任务: python d:\books\programming Languages\python\Projects.vscode\tasks.json <C:\Users[myuser]\Anaconda3\python.exe: 无法打开文件 'd:\books\programming': [Errno 2] 没有那个文件或目录 终端进程以退出代码1终止` - FSm
2
我可以建议使用以下代码:"command": "${config:python.pythonPath} ${file}"。将python替换为config变量将使用在python VS Code扩展中设置的相同pythonPath。如果在Windows上运行,这将使用cmd.exe或PowerShell。根据使用哪个(我认为PowerShell是适用于Win 10或更高版本),在$file变量周围放置单引号或双引号,即:"command":"${config:python.pythonPath} '${file}'"。 - Rafael Zayas
1
很棒的配置,运行得非常好。只想补充一下,他们最近将 taskName 更改为 label,在 VS Code 中只是警告而已,不是什么大问题。 - Yoan Dinkov
我进行了一项小修改,因为使用Python解释器的最新方法从venv或文件夹设置已经过时。请参见此处以获取更多信息:https://github.com/microsoft/vscode-python/issues/11789#issuecomment-631413067。新的方法是:"command": "${command:python.interpreterPath} ${file}" - ferreiradev
${command:python.interpreterPath} 这一部分 :thumbsup: - undefined

6

...没有足够的声望来评论被接受的答案...

至少在我的环境中(Ubuntu 18.04,带有虚拟环境),如果使用“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
            }
        }
    ]
}

这花了我一些时间才搞明白,所以我想分享一下。

4

以下是我用于构建(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
            }
        }
    ]
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接