45

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
  • 453
  • 1
  • 4
  • 7
  • 1
    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

8 Answers8

65

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,850
  • 5
  • 53
  • 59
Ali Kazemkhanloo
  • 1,017
  • 1
  • 10
  • 16
30

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
  • 22,678
  • 7
  • 27
  • 43
10

Just type

call .\venv\Scripts\activate.bat

in the .bat file and any command afterwards will see the venv activated

for the record call in a cmd pauses the execution of the current script, executes the called one and then resumes.

georbbtz
  • 109
  • 1
  • 2
1

your os must be linux or mac because your path is "path/to/Python/proj"

in window, it is "path\to\Python\proj"

in my window machine, scripts below works

call "venv\scripts\activate"
cd ur_path
py script.py
lam vu Nguyen
  • 433
  • 4
  • 9
0

Another way to do this is to make a shortcut of the batch file and then change the "Start in" field. After that remember to use the full paths in your batch file since it will be running from a difference location.

enter image description here

guidout
  • 322
  • 1
  • 4
  • 13
0

Actually, you don't have to activate virtual environment. In the batch file, start the python script by using your virtual environment's python path. i.e

 start C:\mypythonproject\Scripts\python C:\mypythonproject\mycode.py

And in the task scheduler, just schedule your batch file as a regular file.

hakan b
  • 1
  • 1
-1

Create .bat file

write virtual environment activate script location and python file location as below use '&' operator to run two commands.

as below:

"E:\Call Allocation Engine\Development\development_env\Scripts\"activate & python run.py

https://i.stack.imgur.com/31Gkh.png

finally place this file in desired folder and run using cmd.

E:\Call Allocation Engine\Development\Optimisation\Scheduling>file_name.bat

this script will activate virtual environment and run your python code in that environment.

-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
  • 1,070
  • 10
  • 8