Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a python script in virtual environment from windows task scheduler

Tags:

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?

like image 774
deseosuho Avatar asked Jan 05 '16 22:01

deseosuho


People also ask

How do I run a Python script in virtual environment Windows?

Virtual environments in Python 2To install virtualenv, just use pip install virtualenv . To create a virtual environment directory with it, type virtualenv /path/to/directory . Activating and deactivating the virtual environment works the same way as it does for virtual environments in Python 3 (see above).

How do I run a Python script automatically?

Double-click Add Scheduled Task (or Create Basic Task). Complete the options on the wizard. These options include when you want the scheduled task to run, the path to the script you want to run, and any arguments to the script.


1 Answers

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.

like image 102
mx0 Avatar answered Sep 30 '22 05:09

mx0