30

I have a script that i run every day and want to make a schedule for it, i have already tried a batch file with:

start C:\Users\name\Miniconda3\python.exe C:\script.py

And im able to run some basic python commands in it, the problem is that my actual script uses some libraries that were installed with Anaconda, and im unable to use them in the script since Anaconda will not load.

Im working on windows and can't find a way to start Anaconda and run my script there automatically every day.

Cristian M.
  • 421
  • 1
  • 4
  • 5

4 Answers4

20

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.

Mikko Jaakkola
  • 313
  • 2
  • 8
14

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!

knowa42
  • 403
  • 2
  • 12
  • Thx! i just found another way but your method seems better! – Cristian M. Sep 27 '17 at 01:35
  • thanks so much for posting this, was causing me a total headache – REdim.Learning Dec 04 '18 at 10:38
  • But in this way some modules may not be loaded, for example I'm using requests, I'm getting the error `"Can't connect to HTTPS URL because the SSL module is not available."`, I had to call the `.....\activate.bat` file before as mentioned by @HarshaLimaye – Amer Sawan Apr 27 '21 at 22:44
8

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.

Harsha Limaye
  • 915
  • 9
  • 29
1

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

Cristian M.
  • 421
  • 1
  • 4
  • 5