6

I have a fairly large python/c++ program that runs as follow:

  1. . set_env.sh -option A -option B
  2. python run.py

The set_env.sh script modifies the PYTHONPATH and does all sort of export in order to point to the right c++ program. When running these two commands in the terminal, that works just well, however, using the debugger breaks it all.

I try running ". set_env.sh -option A -option B" in preLaunchTask but it seems the debugger can't see the new PYTHONPATH.

How can I make the debugger run the the set_env and consider the new PYTHONPATH ?

Nirvananas
  • 63
  • 1
  • 5

2 Answers2

5

I had a similar problem and could solve it by setting environment variables in the "env" field of the launch.JSON file. This shoudl be stored in a .vscode folder in your project root, here is the docs: https://code.visualstudio.com/docs/python/debugging

I discovered the "env" field in this snippet here: https://gist.github.com/slaveofcode/b4c18efc99c16029913cde3fd2dba3ce

Not sure how to configure this dynamically with the -option A -option B however. Hope this helps :)

fitzberg
  • 333
  • 3
  • 10
  • We are close to the solution but it does not work since the script `set_env.sh` has to be launched before each debugging session :/ – Welgriv Feb 08 '23 at 14:25
5

Inspired by my current area of work - which involved Python.

This is what I do for Flask.

enter image description here

MS Link

https://code.visualstudio.com/docs/python/debugging

For any regular Python debugging

enter image description here

Evidence

import os

for key in os.environ.keys():
    print(f"{key}={os.environ.get(key)}")

After doing a F5 in VS Code with a file containing the above snippet: enter image description here

Update on Feb 18

After receiving a comment I think the OP wants to run a script before debugging the Fask application. The steps that worked for me are below.

Overview

I ended up with the following files:

  • Add a PowerShell file dosomething.ps1 (my custom script which I intend to launch before every debug session)
  • Add a task.json (launch dosomething.ps1)
  • Edit the existing launch.json by linking it with the task in task.json enter image description here

The file dosomething.ps1

Write-Host "Inside do something"
Write-Host "The value of the environment variable TEMP is $env:TEMPcls"
Write-Host "The current path of the script is $PSScriptroot"

The file tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "my script task",
            "type": "shell",
            "windows": {
                "command": "pwsh -f dosomething.ps1"
            },
            "problemMatcher": []
        }
    ]
}

Linking the launch.json with task.json

enter image description here

After pressing F5

enter image description here

Sau001
  • 1,451
  • 1
  • 18
  • 25
  • This is the same answer as @fitzberg but it does not work since the script `set_env.sh` has to be launched before each debugging session :/ – Welgriv Feb 08 '23 at 14:26
  • @Welgriv Please see my updates to the original answer (section marked with Feb 18). I am launching a PowerShell script before launching the Fask debugger – Sau001 Feb 18 '23 at 12:41
  • I can't verify if this works on Windows but on Mac the script will run but then the debugger command will fail to run and sit waiting to attach. – Fatlad Feb 28 '23 at 23:02