[Python] How to run a task in the poetry shell

Discussão em 'Python' iniciado por Stack, Março 25, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to schedule an automated task that runs a python script "myscript1.py" in VS Code.

    First step is to run the command "poetry shell" which spawns a new shell:

    PS C:\dir1\dir2> poetry shell Spawning shell within C:\dir1\dir2.venv Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved.

    Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

    (dir1-py3.12) PS C:\dir1>

    I put this as the command of the first task. Then I put the actual script as the command of the second task. This creates two tasks, one that opens a Poetry shell and the other that opens a different terminal window.

    My purpose is to spawn the Poetry shell within which to run the "myscript1.py."

    Here are the two files:

    tasks.json:


    {
    "version": "2.0.0",
    "tasks": [
    {
    "label": "spawn_poetry_shell",
    "type": "shell",
    "command": "poetry shell",
    "group": "group1",
    "presentation": {
    "reveal": "always",
    "panel": "new"
    },
    {
    "label": "run_script",
    "type": "shell",
    "command": "python C:\\dir1\\dir2\\myscript1.py",
    "group": "group1",
    "presentation": {
    "reveal": "always",
    "panel": "new"
    },
    "problemMatcher": "$msCompile"
    }
    ]
    }

    settings.json:


    {
    "workbench.colorTheme": "Default High Contrast",
    "terminal.integrated.scrollback": 4000,
    "task.allowAutomaticTasks": "on",
    "tasks": [
    {
    "at": "16 16 * * *",
    "run": "workbench.action.tasks.runTask",
    "args": ["spawn_poetry_shell"]
    },
    {
    "at": "17 16 * * *",
    "run": "workbench.action.tasks.runTask",
    "args": ["run_script"]
    },
    ],
    "workbench.startupEditor": "none",
    }


    The poetry.lock, .venv, .vscode files and the "dir2" directory are located under c:\dir1, whereas the "myscript1.py" script is located under the c:\dir1\dir2\ directory.

    Continue reading...

Compartilhe esta Página