Pyinstaller 生成的 exe 不能与 Windows 任务计划程序一起运行 [英] Pyinstaller generated exe doesn't run with Windows Task Scheduler

查看:53
本文介绍了Pyinstaller 生成的 exe 不能与 Windows 任务计划程序一起运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 Pyinstaller 生成了一个 exe,当我双击它时效果很好,但是当我尝试通过任务调度程序运行它时,它从未运行,但在历史记录中显示操作成功完成".

I generated an exe from Pyinstaller, this works perfectly when I double click it but when I try to run it via Task scheduler it never runs but in the history it shows "The operation completed successfully".

为了确定它是否运行,我在 exe 运行时将一些文本记录到日志文件中,而任务调度程序从未发生过这种情况.

To be sure if it ran, I'm logging some text into a log file when the exe runs which never happens via Task scheduler.

以下是我的 Python 程序的简单片段.

Below is the simple snippet of my Python program.

import os
import threading
import sys 
import time
from datetime import datetime
from dateutil import tz

#Auto-detect zones:
from_zone = tz.tzutc()
to_zone = tz.tzlocal()

logFilespath = 'logs' 
if not os.path.exists(logFilespath):
    os.makedirs(logFilespath)

import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# create a file handler
todayDate = datetime.now()
todayDate = datetime.strftime(todayDate, '%Y%m%d')  
handler = logging.FileHandler('logs/log' + str(todayDate) + '.log')
handler.setLevel(logging.INFO)

# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)

# add the handlers to the logger
logger.addHandler(handler)

logger.info('**** Starting KPI Calculations ****')

我正在使用 Python2.7 并尝试使用 py2exe 但结果相同.

I'm using Python2.7 and tried with py2exe but with the same result.

推荐答案

我遇到了同样的问题.我能够想出一些解决方法来解决这个问题.

I had this same issue. I was able to figure out a bit of a work around that gets this working.

我不明白为什么尝试运行 exe 本身不起作用.我还是想不通.所以相反,我启动了一个 cmd(命令提示符)实例并从那里调用 exe.

I do not understand why trying to run the exe itself doesn't work. I still can't figure that out. So instead, I start an instance of cmd (command prompt) and call the exe from there.

当您定义任务将执行的操作时,您需要使用以下设置:

When you define the action the task will perform, you need to use the following settings:

程序/脚本:将其指向命令提示符的 exe.这通常位于 C:\Windows\System32\

Program/script: Point this to the exe for the command prompt. This is usually found in C:\Windows\System32\

添加参数:输入/C 然后是 exe 名称(我的用引号引起来,因为 exe 名称包含空格,但不确定是否需要).您可以输入完整路径,但我不需要,因为我也设置了 Start In 选项.

Add arguments: type /C and then the exe name (mine was in quotes since the exe name contained spaces, but not sure if that is required). You can put the full path, but I didn't need to because I set the Start In option as well.

Start in:这会告诉 cmd 在哪个目录中打开.我将其指向包含我想要运行的 exe 的目录.这样,我就不必在添加参数"字段中提供完整路径.

Start in: This tells cmd which directory to open in. I pointed this to the directory containing the exe I wanted to run. That way, I didn't have to supply the full path in the Add Arguments field.

举个例子:

程序/脚本:C:\Windows\System32\cmd.exe"

Program/script: "C:\Windows\System32\cmd.exe"

添加参数:/C "myexe.exe"

Add arguments: /C "myexe.exe"

开始于:C:\folder1\folder2\folderWithMyExe\"

Start in: "C:\folder1\folder2\folderWithMyExe\"

这对我有用,并在 exe 完成运行后关闭了命令提示符.

This worked for me and closed the command prompt after the exe was finished running.

这篇关于Pyinstaller 生成的 exe 不能与 Windows 任务计划程序一起运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆