Run python script in virtual environment from windows task scheduler

I am trying to set up a recurring Python task through the Windows Task Scheduler.

I had success when I entered the path to 'python.exe' and specified the path to the script as a parameter for the Windows task scheduler (see screenshot below)

windows task scheduler

However, I want to be able to select a specific virtual environment to run the script. I don’t have much knowledge about venv, and I usually use it by opening cmd and running Scripts \ activ.bat in the right virtual environment directory.

How to "run task x in venvxxx every 24 hours" using the Windows task scheduler?

+7
source share
3

:

c:\__full_path_to_virtualenv__\Scripts\activate.bat && python __full_path_to_python_script__.py

&& 2, 1 .

. ( ) Start in, / .

+6

mx0 , , , Task Scheduler - - . , - . cmd.exe, , python.exe .

, , Reddit, python:

path\to\venv\Scripts\python.exe path\to\script.py

, , , , , python.exe.

+2

, , - - , Windows, . , .bat .

.bat Actions > Start a Program > Program/Script: " ".

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 <arg1> <arg2>

call %venv_root_dir%\Scripts\deactivate.bat
cd %original_dir%
exit /B 1

python your_script.py <arg1> <arg2>... <program name> <arg1> <arg2>...

, , .

0

Source: https://habr.com/ru/post/1622997/


All Articles