我需要帮助配置我的Vs代码,使用Cntrl Shift B运行Python脚本,之前一切正常,直到Vs代码升级到2.0.0版本后,现在它要求我配置构建。我对构建一无所知。
过去当我只需要配置任务运行器时就可以很好地工作了。有关任务运行器的YouTube视频。我似乎无法理解构建的含义。
我需要帮助配置我的Vs代码,使用Cntrl Shift B运行Python脚本,之前一切正常,直到Vs代码升级到2.0.0版本后,现在它要求我配置构建。我对构建一无所知。
过去当我只需要配置任务运行器时就可以很好地工作了。有关任务运行器的YouTube视频。我似乎无法理解构建的含义。
在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
:
第二个任务被配置为默认测试任务,只需在 VS Code 中打开的文件夹中运行 nosetest -v
。
"Run Build Task"(用于绑定快捷键 Ctrl
+Shift
+B
的任务)是配置为默认构建任务的任务 1(参见 group
条目)。
编辑:
由 @RafaelZayas 在评论中提出的建议(这将使用在 VS Code 设置中指定的 Python 解释器而不是系统默认解释器;有关更多信息,请参见他的评论):
"command": "${command:python.interpreterPath} ${file}"
...没有足够的声望来评论被接受的答案...
至少在我的环境中(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
}
}
]
}
以下是我用于构建(Ctrl+Shift+B)的配置:
{
// 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
}
}
]
}
"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 ZayastaskName
更改为label
,在 VS Code 中只是警告而已,不是什么大问题。 - Yoan Dinkov${command:python.interpreterPath}
这一部分 :thumbsup: - undefined