[Python] Python script 30+ min to complete via Task Scheduler, CMD, PWSH. VS Code finishes in...

Discussão em 'Python' iniciado por Stack, Maio 10, 2025.

  1. Stack

    Stack Membro Participativo

    I'm encountering a puzzling performance issue with a Python script on Windows:

    The Problem: Python script runs daily and takes over 30 minutes to complete when executed via Windows Task Scheduler, CMD (Admin or not), or PowerShell. However, the exact same script finishes in less than 6 minutes when I run it directly within Visual Studio Code (using the standard green play button in the integrated terminal).

    This dramatic difference in execution time (5x or more) is consistent across multiple runs.

    Setup:

    • Operating System: [Windows 11 Pro]
    • Python Version: [Python 3.13.1]
    • VS Code Version: [VS Code 1.1 (Current and updated)]

    Task Scheduler Configuration:

    • Action: Start a program
    • Program/script: "C:\Program Files\Python\python.exe"
    • Add arguments: "C:\Users\User\script.py"
    • Start in: C:\Users\User\
    • User Account: User (Run only when user is logged on)
    • Privileges: Run with highest privileges

    Execution Methods & Times:

    • VS Code (direct run/integrated terminal): < 6 minutes
    • Prompt (CMD/PWSH): python C:\Users\User\script.py -> 30+ minutes
    • Windows Task Scheduler: Runs the configured action -> 30+ minutes

    Troubleshooting Steps Attempted:

    • Verified the Python executable path used by Task Scheduler/CMD/PS is the same one used by VS Code.
    • Confirmed the "Start in" directory is correctly set in Task Scheduler.
    • Ran the script directly from CMD/PowerShell in the script's directory (C:\Users\User\) using just python script.py - still slow.
    • Checked Task Manager during execution to see if CPU, memory, or disk usage spiked differently (found "no significant differences" or "high disk usage"]).
    • Simplified the script temporarily to just perform a basic task (e.g., print "Hello world" and exit) - ran instantly in all environments (this would indicate the issue is within the script's main logic/dependencies).

    My Question: What could be causing this drastic difference in execution time between running the script in VS Code's environment versus standard Windows CLI environments (CMD, PowerShell) and Task Scheduler? Are there specific environment variables, path configurations, process priorities, resource limitations, or other factors that could explain this behavior? How can I further identify the root cause and make modifications so the script will run across all environments the same?

    Any insights or suggestions for troubleshooting steps would be greatly appreciated!

    Continue reading...

Compartilhe esta Página