Schedule a Python script via batch on windows (using Anaconda)

徘徊边缘 提交于 2019-12-03 06:54:31

I'd recommend creating an Anaconda environment with the packages you need, then using the python from that environment to run your script. Read about Anaconda environments here

For example...

Say you create an environment called cristians_env

conda create --name cristians_env

and you install the packages you need

conda install pandas

Then, all you need to do is this from your batch script (assuming you use Anaconda 2)

start C:\Users\name\Anaconda2\envs\cristians_env\bin\python C:\script.py

and voila! You're using your anaconda environment from your batch script!

I would be a bit careful in calling python directly through environment as one never knows if the internals for activate function has changed.

I'm just using basic bat-script to help me out.

SET log_file=%cd%\logfile.txt
call C:\Anaconda3\Scripts\activate.bat
cd \script_directory
python script.py arg1 arg2 > %log_file%

This script saves the log-file wherever the bat is run from, calls the right environment through activate (in this case the standard) and directs all the stdout into log-file for further investigation.

Then just point your Windows Task Scheduler to the script and set the home directory where you want the log-file to appear.

I had a similar problem a few days ago. What I discovered is that anaconda prompt is nothing but your usual cmd prompt after running an 'activate.bat' script which is located in the anaconda 'Scripts' folder. So to run your python scripts in anaconda all you need to do is write 2 lines in a batch file. (Open notepad and write the lines mentioned below. Save the file with .bat extension)

  1. call C:\....path to anaconda3\Scripts\activate.bat
  2. call python C:\path to your script\Script.py

Then you schedule this batch file to run as you wish and it will run without problems.

Found a solution, i copied the "activate.bat" file in "C:\Users\yo\Miniconda3\Scripts" and renamed it as schedule.bat and added my script (copy pasted it) on the end of the file.

Then i can schedule a task on windows that executes schedule.bat everyday

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!