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

后端 未结 4 1705
别跟我提以往
别跟我提以往 2020-12-09 04:29

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

相关标签:
4条回答
  • 2020-12-09 04:49

    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.

    0 讨论(0)
  • 2020-12-09 04:50

    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!

    0 讨论(0)
  • 2020-12-09 04:59

    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.

    0 讨论(0)
  • 2020-12-09 05:05

    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

    0 讨论(0)
提交回复
热议问题