Run a python script in virtual environment from windows task scheduler

后端 未结 5 1714
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 20:48

I\'m trying to set up a recurring Python task through windows task scheduler.

I have had success when I input the path to \'python.exe\' and provide the script\'s pa

5条回答
  •  死守一世寂寞
    2020-12-03 21:42

    This is more verbose but very easy to understand, and - I found the most important - much easier than using Windows Task Scheduler settings when you have lots of scripts. To create another you just copy the .bat file and change one line.

    Save this as a .bat file and point to it under Actions > Start a Program > Program/Script:, with no arguments or "Start in" necessary.

    set original_dir=%CD%
    set venv_root_dir="C:\Python-Venvs\env-name"
    cd %venv_root_dir%
    call %venv_root_dir%\Scripts\activate.bat
    
    python your_script.py  
    
    call %venv_root_dir%\Scripts\deactivate.bat
    cd %original_dir%
    exit /B 1
    

    For an installed command-line program, you can replace python your_script.py ... with ....

    In addition it's simple to add another script on the following line, rather than attempting to parse sequential scripts into a one-liner for Task Scheduler.

提交回复
热议问题