【问题标题】:Pyinstaller generated exe doesn't run with Windows Task SchedulerPyinstaller 生成的 exe 不能与 Windows 任务计划程序一起运行
【发布时间】:2017-09-08 19:53:34
【问题描述】:

我从 Pyinstaller 生成了一个 exe,当我双击它时它运行良好,但是当我尝试通过任务调度程序运行它时它永远不会运行,但在历史记录中它显示“操作成功完成”。

为了确定它是否运行,我在 exe 运行时将一些文本记录到日志文件中,而这绝不会通过任务调度程序发生。

下面是我的 Python 程序的简单 sn-p。

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 但结果相同。

【问题讨论】:

    标签: python-2.7 pyinstaller py2exe taskscheduler


    【解决方案1】:

    我也有同样的问题。我能够想出一些解决方法来解决这个问题。

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

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

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

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

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

    举个例子:

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

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

    开始于:“C:\folder1\folder2\folderWithMyExe\”

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

    【讨论】:

      【解决方案2】:

      解决这个问题的简单方法:

      1.在您的系统中找到 python 可执行文件。

      import sys
      print("Python EXE: " + sys.executable)
      

      在我的例子中:“C:\users\USER\appdata\local\continuum\anaconda3\python.exe”

      2。选择 Python 代码的路径。

      在我的例子中:“C:\Users\USER\Documents\SandBox\test.py”

      3.打开记事本并写下:'start PYTHONPATH SCRIPTPATH',如:

      start C:\users\USER\appdata\local\continuum\anaconda3\python.exe C:\Users\USER\Documents\SandBox\test.py
      

      4.将此记事本另存为 .bat 文件。

      5.安排这个 .bat

      系统将正确运行将运行脚本的bat。

      【讨论】:

      • 这似乎没有回答如何运行pyinstaller创建的exe,只是如何运行python代码。如果没有 python 的计算机需要这个(因此需要 exe),它并不能解决问题。
      猜你喜欢
      • 2022-12-14
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 2017-08-01
      • 2014-12-05
      • 1970-01-01
      相关资源
      最近更新 更多