Python 在 Windows 10 中的 Task Scheduler 上安排 .py 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44727232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:24:26  来源:igfitidea点击:

Scheduling a .py file on Task Scheduler in Windows 10

python

提问by Stef

I already tried to convert my .py file into .exe file. Unfortunately, the .exe file gives problems; I believe this is because my code is fairly complicated. So, I am trying to schedule directly my .py file with Task Scheduler but every time I do it and then run it to see if works, a window pops up and asks me how I would like to open the program?-.-

我已经尝试将我的 .py 文件转换为 .exe 文件。不幸的是,.exe 文件会出现问题;我相信这是因为我的代码相当复杂。所以,我试图用 Task Scheduler 直接安排我的 .py 文件,但每次我这样做然后运行它以查看是否有效时,会弹出一个窗口并询问我想如何打开该程序?-.-

Does any of you know how I can successfully schedule my .py file with Task Scheduler? Please help, thanks

你们中有人知道如何使用任务计划程序成功安排我的 .py 文件吗?请帮忙,谢谢

Windows 10 Python 3.5.2

Windows 10 Python 3.5.2

回答by Dragon

Creating the exe should be the best method. But if you want to run it with the task scheduler you can do it in this way:

创建exe应该是最好的方法。但是如果你想用任务调度程序运行它,你可以这样做:

  1. Launch Window's Task Scheduler
  2. Look for the The Actions pane(on the right) it has the Create Basic Task action. Click on it.
  3. This will open a wizard where you will define the name of your task, the trigger (when it runs), and the action (what program to run). Action tab is where you specify the name of your Python script to run as well as any arguments to the script.
  1. 启动窗口的任务计划程序
  2. 查找操作窗格(在右侧),它具有创建基本任务操作。点击它。
  3. 这将打开一个向导,您将在其中定义任务的名称、触发器(运行时)和操作(要运行的程序)。操作选项卡是您指定要运行的 Python 脚本的名称以及脚本的任何参数的地方。

To ensure that your Python script will run regardless of the login account that the schedule task uses, and to avoid any confusion about which version of Python is used in mixed environments (64bit or 32bit), it is recommended that you run the Python executable with the name of your Python file as an argument to the executable.

为确保无论计划任务使用的登录帐户如何,您的 Python 脚本都将运行,并避免混淆在混合环境(64 位或 32 位)中使用哪个版本的 Python,建议您运行 Python 可执行文件Python 文件的名称作为可执行文件的参数。

Suppose the script you want to run is E:\My script.py. Instead of running the script directly, instruct the task scheduler to run python.exe with the script as an argument.For example:

假设您要运行的脚本是 E:\My script.py。不是直接运行脚本,而是指示任务调度程序以脚本作为参数运行 python.exe。例如:

C:\Python27\ArcGIS10.2\python.exe "E:\My script.py"

C:\Python27\ArcGIS10.2\python.exe "E:\My script.py"

The location of python.exe depends on your install. If you don't know where it is, you can discover its location; copy and paste the following code into a new Python script then execute the script. The script will print the location of python.exe as well as other information about your Python environment.

python.exe 的位置取决于您的安装。如果你不知道它在哪里,你可以发现它的位置;将以下代码复制并粘贴到新的 Python 脚本中,然后执行该脚本。该脚本将打印 python.exe 的位置以及有关 Python 环境的其他信息。

import sys
import platform
import imp

print("Python EXE     : " + sys.executable)
print("Architecture   : " + platform.architecture()[0])
print("Path to arcpy  : " + imp.find_module("arcpy")[1])

raw_input("\n\nPress ENTER to quit")

After determining the location of python.exe, this is what is entered in the Action panel of the task scheduler: enter image description here

确定python.exe的位置后,在任务调度器的Action面板中输入的是这样的: 在此处输入图片说明

If there are additional arguments (parameters) to your script, provide them after the path to your script. Hope this helps.

如果您的脚本有其他参数(参数),请在您的脚本路径之后提供它们。希望这可以帮助。

回答by MagTun

You should set in the Actiontab:

您应该在Action选项卡中设置:

  • in programs, the path to your python.exe: for instance "C:\Users\Me\AppData\Local\Programs\Python\Python36\python.exe"
  • in arguments, the full path to your file including the extension: for instance "C:\Users\Me\Desktop\mypythonsrcipt.py"
  • for start in: leave empty
  • 在程序中,python.exe 的路径:例如 "C:\Users\Me\AppData\Local\Programs\Python\Python36\python.exe"
  • 在参数中,文件的完整路径,包括扩展名:例如 "C:\Users\Me\Desktop\mypythonsrcipt.py"
  • 开始:留空

If this doesn't work, try :

如果这不起作用,请尝试:

  • in programs, the path to your python.exe: for instance "C:\Users\Me\AppData\Local\Programs\Python\Python36\python.exe"
  • in arguments, your filename with the extension: for instance "mypythonsrcipt.py"
  • in start in, the folder of you script : for instance "C:\Users\Me\Desktop\"
  • 在程序中,python.exe 的路径:例如 "C:\Users\Me\AppData\Local\Programs\Python\Python36\python.exe"
  • 在参数中,带有扩展名的文件名:例如 "mypythonsrcipt.py"
  • 在开始时,您脚本的文件夹:例如 "C:\Users\Me\Desktop\"

Also each time I modify a task, the user account in the Generaltab is switched to Medium Mandatory Level. So I have to reopen the taks and set the user account back to my username: (cf. this question)

此外,每次修改任务时,General选项卡中的用户帐户都会切换到中强制级别。所以我必须重新打开任务并将用户帐户设置回我的用户名:(参见这个问题

enter image description here

在此处输入图片说明

If you still can't run your script, go in Event Log, Applications and Service Log/Microsoft/Windows/TaskScheduler/Operational(right click it to enable it) and look for the errors.

如果您仍然无法运行脚本,请进入事件日志,Applications and Service Log/Microsoft/Windows/TaskScheduler/Operational(右键单击它以启用它)并查找错误。

回答by user637338

I almost lost my hair over this. I always got 0x1 as the result of doing what is described above. A long experienced Windows admin told me this:

为此我差点掉头发。作为执行上述操作的结果,我总是得到 0x1。一位经验丰富的 Windows 管理员告诉我:

Create a batch file:

创建批处理文件:

SET logfile="C:\Reports\batch.log"
@echo off
@echo Starting Script at %date% %time% >> %logfile%
"C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\python.exe" "C:\Users\sys_www\source\repos\hardware\ReportAssembler.py"
@echo finished at %date% %time% >> %logfile%

Then provide the batch file in the action part of the task config. One thing to also take care of that all the files written during the runtime of the python program can actually be accessed by the user executing the script.

然后在任务配置的操作部分提供批处理文件。还有一件事要注意,在 python 程序运行时写入的所有文件实际上可以被执行脚本的用户访问。

I tried using the script as a parameter and the python exe in programm/script. First I go the error "Windows Scheduled Tasks not running". Then after some configuring around I got the error 0x1, which told me absolutely nothing.

我尝试使用脚本作为参数,并在程序/脚本中使用 python exe。首先,我会出现错误“Windows 计划任务未运行”。然后经过一些配置后,我得到了错误 0x1,它完全没有告诉我什么。

回答by ToCarbajal

Dragon's answer works fine as long as your script not takes much time to finish.

只要您的脚本不需要太多时间来完成,Dragon 的答案就可以正常工作。

I used a little different approach in the Windows task scheduler:

我在 Windows 任务调度程序中使用了一些不同的方法:

In Program/scripttextbox you set the path to Python executable (in my case is inside the virtualenv folder).

Program/script文本框中,您将路径设置为 Python 可执行文件(在我的情况下是在 virtualenv 文件夹中)。

Add arguments= Just the name of your Python script.

Add arguments= 只是您的 Python 脚本的名称。

Start in= The full path of your Python script (without the name.py).

Start in= Python 脚本的完整路径(不带 name.py)。

enter image description here

在此处输入图片说明

This way the script runs, the console stays open and waits until the end.

这样脚本运行时,控制台保持打开状态并等待结束。

回答by Connor

The script you execute would be the exe found in your python directory ex) C:\Python27\python.exe

您执行的脚本将是在您的 python 目录中找到的 exe,例如)C:\Python27\python.exe

The "argument" would be the path to your script ex) C:\Path\To\Script.py

“参数”将是您的脚本的路径,例如)C:\Path\To\Script.py

So think of it like this: you aren't executing your script technically as a scheduled task. You are executing the root python exe for your computer with your script being fed as a parameter.

因此,可以这样想:从技术上讲,您没有将脚本作为计划任务执行。您正在为您的计算机执行根 python exe,并将您的脚本作为参数提供。

回答by Ozcar Nguyen

This worked for me, you could try with the Action pane: The Action pane

这对我有用,您可以尝试使用操作窗格: 操作窗格

Please note that the path of py file in the double quotes, add arguments: $(Arg0)

请注意双引号中py文件的路径,添加参数:$(Arg0)

Check "Run with highest privileges" in the General tab, and it'd better to choose "Run a new instance in parallel" in drop down menu in Settings tab.

在常规选项卡中选中“以最高权限运行”,最好在设置选项卡的下拉菜单中选择“并行运行新实例”。

After all, you could set the schedule for task in Triggers tab to check the effect of changes (remember choose "Run whether user is logged on or not" in the General tab).

毕竟,您可以在“触发器”选项卡中设置任务计划以检查更改的效果(请记住在“常规”选项卡中选择“无论用户是否登录都运行”)。

回答by Maik Holzhey

For some reason, the windows task scheduler starts the python.exe in an environment, where they fail at import-module statements. I had to use a workaround using the CMD.exe and pass the command to run the python script in the given folder.

出于某种原因,windows 任务调度程序在一个环境中启动 python.exe,它们在 import-module 语句中失败。我不得不使用使用 CMD.exe 的解决方法并传递命令以在给定文件夹中运行 python 脚本。

Use the program

使用程序

CMD

then the options tab:

然后是选项选项卡:

/c python main.py

and fill in the source location in the tab Start in:

并在选项卡中填写源位置开始:

C:\Users\...

Not sure what's the reason for that. The previously described solutions all did not work for me.

不知道这是什么原因。之前描述的解决方案都不适用于我。

回答by Emmy

This absolutely worked for me . I am using windows 10 professional edition and it has taken me almost 6 months to get this solution.Thanks to the suggestion made above.

这绝对对我有用。我使用的是 Windows 10 专业版,我花了将近 6 个月的时间才得到这个解决方案。感谢上面提出的建议。

I followed this suggestion and it worked right away and smoothly. All I did was to instruct the scheduler to run python.exe with my script as an argument just as explained by this fellow below

我遵循了这个建议,它立即顺利运行。我所做的只是指示调度程序以我的脚本作为参数运行 python.exe,正如下面这个人所解释的

This what I did Suppose the script you want to run is E:\My script.py. Instead of running the script directly, instruct the task scheduler to run python.exe with the script as an argument. For example:

这就是我所做的 假设您要运行的脚本是 E:\My script.py。不是直接运行脚本,而是指示任务调度程序以脚本作为参数运行 python.exe。例如:

C:\Python27\ArcGIS10.2\python.exe

C:\Python27\ArcGIS10.2\python.exe

"E:\My script.py"

“E:\我的脚本.py”

The location of python.exe depends on your install. If you don't know where it is, you can discover its location; copy and paste the following code into a new Python script then execute the script. The script will print the location of python.exe as well as other information about your Python environment.

python.exe 的位置取决于您的安装。如果你不知道它在哪里,你可以发现它的位置;将以下代码复制并粘贴到新的 Python 脚本中,然后执行该脚本。该脚本将打印 python.exe 的位置以及有关 Python 环境的其他信息。