How to Coexist Powershell Debugs and Python Debugs in VScode

Asked 2 years ago, Updated 2 years ago, 407 views

  • Window 10pro
  • Vscode 1.70.2
  • Python 3.9.10<-python interpreter selected under virtual environment/ENV folder by python venv
  • Powershell Extension v2022.7.2
  • Vscode enhancements
     PowerShell  Python
     Python Extended
     Python Extension Pack
     Python Docs
    Code Runner ...etc

Since it is usually a Python development, F5 debugging runs on Python in a Python virtual environment.
It also automates documentation with Powershell scripts.I used CodeRunner to run the Powershell script, but the script that was running so far failed for some reason, so I wanted to debug it and chase it.
However, if you open the Powershell script and click Run - > Start Debugging as the current file, you will try to run it as a Python script.

Runtime Terminal View

(ENV)PS [Current Folder] > cd '[Current Folder] '; & '[Current Folder]\ENV\Script\python.exe' [(omitted)..\pythonFiles\lib\debugpy\adapter/../..\debugpy\launcher] '55292' ' --' [Current File]'

The current file above is the ps1 file in this case.

·Determine the ps1 file and run Powershell debugging
·Determine the py file and execute Python debugging
This is a consultation about whether it is possible to create an environment where

{
    // Use IntelliSense to learn the available attributes.
    // Displays a description of an existing attribute by hovering.
    // For more information: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode"—true
        }
    ]
}
{
  "esbonio.server.enabled": true,
  "esbonio.spinx.confDir":",
  "restructuredtext.preview.name": "docutils",
  "files.encoding": "utf8bom"
}

python vscode powershell debugging

2022-09-30 22:04

1 Answers

If you use Powershell very rarely, I think it's simple to add a Powershell configuration to launch.json and select from the combo box to the right of Run and Debug in the upper left corner of the `launch.json screen

Automatic file determination with extensions can be accomplished by using the Command Variable extension.
In the launch.json example below, you can automatically determine .ps1 and .py by selecting Debug Python or Powershell from the combo box to the right of Run and Debug.
However, the terminal does not switch automatically, so you need to manually switch from the vertical tab in the lower right corner of the screen.

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File if.py",
            "type": "python",
            "request": "launch",
            "program": "${input:file_if_python}",
            "console": "integratedTerminal",
            "justMyCode"—true
        },
        {
            "name": "PowerShell: Current File if.ps1",
            "type": "PowerShell",
            "request": "launch",
            "script": "${input:script_if_powershell}",
            "cwd": "${input:script_if_powershell}" 
        }
    ],
    "compounds": [
      {
        "name": "Debug Python or Powershell",
        "configurations": ["Python: Current File if.py", "PowerShell: Current File if.ps1" ]
      }
    ],
    "inputs": [
      {
        "id": "file_if_python",
        "type": "command",
        "command": "extension.commandvariable.file.fileAsKey",
        "args": {
          ".py": "${file}",
          ".ps1": "${workspaceFolder}\\.vscode\\dummy" 
        }
      },
      {
        "id": "script_if_powershell",
        "type": "command",
        "command": "extension.commandvariable.file.fileAsKey",
        "args": {
          ".ps1": "${file}",
          ".py": "${workspaceFolder}\\.vscode\\dummy" 
        }
      }
    ]
}

Screenshot

References


2022-09-30 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.