admin管理员组

文章数量:1399805

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!

(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.

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.

Share Improve this question edited Mar 25 at 18:05 Naveed Ahmed 5052 silver badges13 bronze badges asked Mar 24 at 21:54 RembergneRembergne 91 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

The poetry shell starts a new subshell and blocks subsequent commands.

Run commands directly using poetry:

 "command": "poetry.exe",
        "args": [
          "run",
          "python",
          "myscript1.py"
        ],

本文标签: pythonHow to run a task in the poetry shellStack Overflow