VSCode with Python, execute command prior to venv

216 Views Asked by At

Due to company constraints I cannot immediately execute PowerShell commands:

File C:...Activate.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see 
about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170

The workaround I already found (thanks Stack!) is to execute, in the command line (not as a PS script):

Set-ExecutionPolicy Unrestricted -Scope Process

This solves the problem and when I'm running code for real, it works well. When I am in the middle of a development/debugging session in VS Code, however, this command does not execute.

Is there a place in one of the JSON setup files where I can pre-execute the above command before PS or cmd or the python debugger attempts to run Activate.ps1? This fixes venv problems for me and anyone else with problematic corporate script execution policies.

1

There are 1 best solutions below

1
JialeDu On

VS Code supports adding tasks.json files to set up automatic tasks.

A simple example:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "test",
            "type": "shell",
            "command": "dir",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": false
            },
            "runOptions": {
                "runOn": "folderOpen"
            }
        }
    ]
}

This setting is to run the dir command to read the files in the directory when opening the folder.

enter image description here

How to create a task.json file?

  1. Open command palette with Ctrl+Shift+P

  2. Search and select Tasks:Configure Task

    configure task

  3. Choose Create task.json file fromtemplate

    creshhssshsshfhate

  4. Choose Others

    enter image description here

More about tasks.json: Click here

PS:

Regarding debugging, the launch.json file can also help a lot. Click here and here to know more.