前言
有时我们使用VScode的使用想方便的启动项目,并且启动项目之前可能要执行一些前置的命令,我们就可以用VSCode的任务来实现。
简单任务
项目根目录创建.vscode文件夹
添加文件tasks.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| { "version": "2.0.0", "tasks": [ { "label": "主页面", "type": "shell", "command": "python main.py", "problemMatcher": [], "group": { "kind": "build", "isDefault": true } } ] }
|
运行任务直接用快捷键Ctrl+Shift+B
运行时再点击快捷键Ctrl+Shift+B可以选择重启任务或者终止任务
带依赖的任务
先编译后执行脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| { "version": "2.0.0", "tasks": [ { "label": "编译", "type": "shell", "command": "pyside2-rcc resources.qrc -o resources_rc.py", "problemMatcher": [], }, { "label": "主页面", "type": "shell", "command": "python main.py", "dependsOn": [ "编译" ], "problemMatcher": [], "group": { "kind": "build", "isDefault": true } } ] }
|
使用虚拟环境的Python
默认VSCode的任务使用的是环境变量中的Python,如果我们想用项目的虚拟环境,可以指定对应的位置的Python。
其中${workspaceFolder}获取的是项目的根目录。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| { "version": "2.0.0", "tasks": [ { "label": "编译", "type": "shell", "command": "${workspaceFolder}/.venv/Scripts/pyside2-rcc resources.qrc -o resources_rc.py", "problemMatcher": [], }, { "label": "主页面", "type": "shell", "command": "${workspaceFolder}/.venv/Scripts/python main.py", "dependsOn": [ "编译" ], "problemMatcher": [], "group": { "kind": "build", "isDefault": true } } ] }
|