31

I'd like to use Windows Task Scheduler to run a python script within a virtual environment. I'd like the Scheduler to run a .bat file that will

  1. activate the virtualenv
  2. run the script

These steps work together from the command line, and they work individually in a .bat, but I can't seem to get them to work together from the .bat. It seems the virtualenv is not fully activated when I try to execute the python script and confused as to why.

My .bat looks like this:

call workon venv
cd path/to/Python/proj
python -m script.py

I've tried adding timeouts immediately after the call to workon and tried moving the workon to seperate .bat called from my first file, but the other lines still execute before the virtualenv is activated. Any help is greatly appreciated!

Clark D
  • 313
  • 1
  • 3
  • 7
  • this answer might be similar and helpful [A python script that activates the virtualenv and then runs another python script?](https://stackoverflow.com/a/30927921/1248974) – chickity china chinese chicken Nov 22 '17 at 02:02
  • 2
    I saw that thread earlier but wasn't aware of the importance of chaining the statements together with the `&` operator as @Gerhard pointed out below. Including the `&` operator resolved this issue for me. – Clark D Nov 22 '17 at 17:28

4 Answers4

36

You do not need to activate the virtual environment while running in .bat. All you need to do is to run the python.exe file in your virtual environment.

{path to virtual environment directory}/Scripts/python.exe path/to/your/file.py

In Windows Task Scheduler you can specify the path in which the command prompt will open. So all you need to do is when adding the action, use path to your python in the field Program/script, the name of the file to be run in Add arguments field, and the path to your file.py in Start in field.

windows task scheduler example

P.S if you are reading or writing files in your python file, note that your path will be relative to the one you specify in your start in field in the Action window

sr9yar
  • 4,072
  • 4
  • 49
  • 54
Ali Kazemkhanloo
  • 640
  • 6
  • 15
21

You can use an ampersand & operator in a oneliner batch file.

call workon venv & cd path/to/Python/proj & python -m script.py

It will run each command after the other.

You can also double up the ampersand to make it a conditional operator. &&:

call workon venv && cd path/to/Python/proj && python -m script.py

Here the command will only run, if the previous command completed successfully, in other words ERRORLEVEL = 0

Gerhard
  • 21,163
  • 7
  • 24
  • 41
-3

Edit activate.bat and place this line at the bottom:

python yourscript.py

Schedule the activate.bat itself and it will automatically run your script after the virtual environment activated.

hlorand
  • 712
  • 6
  • 7
-8

Just execute the activate.bat as below enter image description here

RickeyShao
  • 49
  • 1
  • 1
  • 7
  • 2
    I *guess* your answer has been downvoted because your do not exactly answer the question: the OP did not ask how to activate a venv from command line, but from a script. However, your solution to activate the virtualenv does work from a batch script: on can write something like `\path\to\myvenv\Scripts\activate.bat` – zezollo Oct 29 '20 at 12:42