Run a python script in virtual environment from windows task scheduler

deseosuho picture deseosuho · Jan 5, 2016 · Viewed 17.4k times · Source

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 path as a parameter to windows task scheduler (see screenshot below)

windows task scheduler

However, I want to be able to choose a particular virtual environment in which to run the script. I don't have much knowledge of venv, and I typically use it by opening cmd and running Scripts\activate.bat in the desired virtual environment directory.

How can I accomplish 'run task x in venvxxx every 24 hours' using windows task scheduler?

Answer

mx0 picture mx0 · May 15, 2017

Create batch file with these commands:

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

&& means run command2 if command1 completed successfully.

Then set that batch file as script to run. You don't need to set any additional arguments in task scheduler (or you can set them in batch file anyway) and can set Start in if script has to read/write from specific directory and uses relative paths.