Activate virtualenv and run .py script from .bat

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

Leave a Comment