Activate virtualenv and run .py script from .bat
Question:
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
- activate the virtualenv
- 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!
Answers:
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
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.
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
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.
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 EngineDevelopmentdevelopment_envScripts"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 EngineDevelopmentOptimisationScheduling>file_name.bat
this script will activate virtual environment and run your python code in that environment.
Just type
call .venvScriptsactivate.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.
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
- activate the virtualenv
- 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!
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
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.
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
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.
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 EngineDevelopmentdevelopment_envScripts"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 EngineDevelopmentOptimisationScheduling>file_name.bat
this script will activate virtual environment and run your python code in that environment.
Just type
call .venvScriptsactivate.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.