在 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}"