简体   繁体   English

在Windows上通过批处理计划Python脚本(使用Anaconda)

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

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. 而且我无法在其中运行一些基本的python命令,问题是我的实际脚本使用了随Anaconda安装的一些库,由于无法加载Anaconda,因此我无法在脚本中使用它们。

Im working on windows and can't find a way to start Anaconda and run my script there automatically every day. 我在Windows上工作,找不到每天自动启动Anaconda并在那里运行我的脚本的方法。

I'd recommend creating an Anaconda environment with the packages you need, then using the python from that environment to run your script. 我建议使用所需的软件包创建Anaconda环境,然后使用该环境中的python运行脚本。 Read about Anaconda environments here 在这里阅读有关Anaconda环境的信息

For example... 例如...

Say you create an environment called cristians_env 假设您创建了一个称为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) 然后,您所需要做的就是从批处理脚本中执行此操作(假设您使用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! 您正在从批处理脚本中使用anaconda环境!

I would be a bit careful in calling python directly through environment as one never knows if the internals for activate function has changed. 我直接在环境中调用python会有些谨慎,因为永远不知道Activate函数的内部是否已更改。

I'm just using basic bat-script to help me out. 我只是使用基本的bat脚本来帮助我。

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. 然后,只需将Windows Task Scheduler指向该脚本,并设置要在其中显示日志文件的主目录即可。

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. 我发现在运行anaconda'Scripts'文件夹中的'activate.bat'脚本后, anaconda提示符不过是您通常的cmd提示符 So to run your python scripts in anaconda all you need to do is write 2 lines in a batch file. 因此,要在anaconda中运行python脚本,您要做的就是在批处理文件中写入2行。 (Open notepad and write the lines mentioned below. Save the file with .bat extension) (打开记事本并编写以下提到的行。用.bat扩展名保存文件)

  1. call C:\\....path to anaconda3\\Scripts\\activate.bat 调用C:\\ .... anaconda3 \\ Scripts \\ activate.bat的路径
  2. call python C:\\path to your script\\Script.py 调用python C:\\您的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. 找到一个解决方案,我将“ activate.bat”文件复制到“ C:\\ Users \\ yo \\ Miniconda3 \\ Scripts”中,并将其重命名为schedule.bat,并在文件末尾添加了我的脚本(复制粘贴)。

Then i can schedule a task on windows that executes schedule.bat everyday 然后,我可以安排在每天执行schedule.bat的Windows上的任务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM