Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 988
Cristian M. Avatar asked Sep 27 '17 00:09

Cristian M.


People also ask

How do I schedule a Python script in Windows?

In order to schedule the Python script using the Windows Scheduler: Open the Windows Control Panel and then click on the Administrative Tools. Double-click on the Task Scheduler, and then choose the option to 'Create Basic Task…' Type a name for your task (you can also type a description if needed), and then press Next ...

How do I run a Python script from Anaconda prompt?

How to Run a Python Script. If you created the file in the Anaconda Spyder IDE, you can run the script by clicking on the green triangle (the Run button) in the upper-lefthand corner of the IDE. When you click the Run button, you'll see the output displayed in the Python console in the lower-righthand pane of the IDE.

How do you automate a schedule in Python?

First, create your Python script. Then, open the system terminal your working with. To access crontab, input 'crontab -e' (one will be created if it doesn't already exist). Then enter 'i' to initiate the edit mode, and proceed to input your schedule command.


Video Answer


1 Answers

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.

like image 65
Mikko Jaakkola Avatar answered Sep 22 '22 23:09

Mikko Jaakkola