vscode调试代码2(提前运行程序)

今天遇到一个很想解决的问题哈,就是如何使用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

  1. Open your project in VSCode.

  2. Create a tasks.json file:

    • Go to the Command Palette (Ctrl+Shift+P or Cmd+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.
  3. Define the task:
    In the tasks.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

  1. Open or create a launch.json file:

    • Go to the Run and Debug view (Ctrl+Shift+D or Cmd+Shift+D on macOS).
    • Click on “create a launch.json file” if you don’t have one, or open the existing launch.json file.
  2. Add or modify the debug configuration:
    In your launch.json file, modify the configuration to include the preLaunchTask 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 your tasks.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就会提前运行了,这个还是很有用的

要想看终端的结果,需要看右边
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值