今天遇到一个很想解决的问题哈,就是如何使用vscode在调试前运行一个脚本,这个相当于提前运行的任务
之所以之前这个问题没有解决,是因为我搜索的
How to run a script before running/debugging a python script by vscode?
To run a script before running or debugging a Python script in Visual Studio Code (VSCode), you can configure tasks in the tasks.json
file and use the preLaunchTask
option in your launch.json
configuration. Here’s a step-by-step guide:
Step 1: Create a Task to Run Your Script
-
Open your project in VSCode.
-
Create a
tasks.json
file:- Go to the Command Palette (
Ctrl+Shift+P
orCmd+Shift+P
on macOS). - Type and select “Tasks: Configure Task.”
- Choose “Create tasks.json file from template.”
- Select “Others” as the task type.
- VSCode will create a
.vscode/tasks.json
file in your project.
- Go to the Command Palette (
-
Define the task:
In thetasks.json
file, define the task that will run your script. Here’s an example:{ "version": "2.0.0", "tasks": [ { "label": "Run Pre-Script", "type": "shell", "command": "python path/to/your/script.py", "problemMatcher": [] } ] }
Replace
"path/to/your/script.py"
with the actual path to the script you want to run before your main Python script.
Step 2: Modify the Debug Configuration
-
Open or create a
launch.json
file:- Go to the Run and Debug view (
Ctrl+Shift+D
orCmd+Shift+D
on macOS). - Click on “create a launch.json file” if you don’t have one, or open the existing
launch.json
file.
- Go to the Run and Debug view (
-
Add or modify the debug configuration:
In yourlaunch.json
file, modify the configuration to include thepreLaunchTask
field. Here’s an example:{ "version": "0.2.0", "configurations": [ { "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "preLaunchTask": "Run Pre-Script" } ] }
Ensure that
"preLaunchTask"
matches the"label"
you defined in yourtasks.json
file.
Step 3: Run or Debug Your Python Script
- Now, when you run or debug your Python script using this configuration, VSCode will automatically run the pre-launch task before starting your main Python script.
This setup is particularly useful when you need to perform setup tasks like initializing a database, starting a server, or any other prerequisite actions before running your main script.
我的操作如下
首先创建一个文件夹test_debug
step1: 创建一个tt.py和pretask.py
tt.py
import numpy as np
for i in range(10):
print(i)
print("done!")
pretask.py
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-2*np.pi,2*np.pi)
y = np.sin(x)
plt.plot(x,y)
plt.savefig("./aaa.png")
step2: 设置task.json和launch.json文件
create launch.task
ctrl+shift +P 或者点击齿轮+命令面板
然后选择
然后会打开一个文件
然后修改task.json的内容如下
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Run Pre-Script",
"type": "shell",
"command": "python /Users/yxk/Desktop/test/test_debug/pretask.py"
}
]
}
launch.json
然后选择python debugger和python文件,就可以打开launch.json的文件,
只需要加入”preLaunchTask“这一行即可,最终的结果如下
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python 调试程序: 当前文件",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"preLaunchTask": "Run Pre-Script"
}
]
}
最终调试tt.py
可以看到这里的aaa.png文件出现了,证明pretask就会提前运行了,这个还是很有用的
要想看终端的结果,需要看右边