[Python] Python program runs fine in terminal but will not run in windows scheduled task

Discussão em 'Python' iniciado por Stack, Dezembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to figure out why my python script will not run in Windows Task Manager. I've scheduled many tasks including even tasks that restart windows, but this problem has me baffled. Here is the part of my python script from the beginning to where it stops running:

    import pandas as pd
    import numpy as np
    import time
    import datetime
    import os
    from pathlib import Path
    import pyodbc
    import sqlalchemy as sql
    import inspect
    import requests
    import configparser
    import re
    import time
    import math
    from io import StringIO
    import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText

    logstuff = dict()
    logstuff['Duplicity'] = 1
    logstuff['basename'] = Path(__file__).stem

    def GetLogFile():
    global logstuff
    LogFileName = os.environ['TEMP'] + '\\TWCO'
    if not os.path.exists(LogFileName):
    os.mkdir(LogFileName)
    # print("In function: logstuff[basename]=" + logstuff['basename'])
    if 'basename' in logstuff.keys():
    LogFileName = LogFileName + "\\" + logstuff['basename'] + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") + ".log"
    else:
    LogFileName = LogFileName + "\\" + Path(__file__).stem + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") + ".log"
    SALog = open(LogFileName, "w")
    SALog.write("Python program " + Path(__file__).stem + " starting...\n")
    logstuff['FileNumber'] = SALog
    logstuff['FileName'] = LogFileName
    return logstuff

    def logsomething(logstring):
    global logstuff
    output = (datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S") + " " + logstring
    if logstuff['FileNumber']:
    logstuff["FileNumber"].write(output + "\n")
    if logstuff['Duplicity'] == 1:
    print(output)
    else:
    print(output)

    def ChangeDate(Date):
    return datetime.datetime.strftime(datetime.datetime.strptime(Date, "%a, %d %b %Y %H:%M:%S"), "%d/%b/%Y %H:%M:%S")

    def MakeBigInt(MyVar):
    if isinstance(MyVar, float):
    if math.isnan(MyVar):
    return 0
    else:
    return int(MyVar)
    else:
    return int(MyVar)

    def MakeString(MyVar):
    if isinstance(MyVar, (float, int)):
    if math.isnan(MyVar):
    return ""
    else:
    return str(MyVar)
    if isinstance(MyVar, str):
    return MyVar

    def MakeFloat(MyVar):
    return float(MyVar)

    start_time = time.time()

    # os.system('cls')
    ComputerName = os.environ['COMPUTERNAME']
    ScriptName = str(Path(__file__))

    GetLogFile()

    logsomething("Reading the configuration files.")
    config = configparser.ConfigParser(interpolation=None)
    logsomething("Loaded ConfigParser")
    config.read("//pri-lvm-na01/export_five9/Util/Five9CredsSA.ini")
    logsomething("Opened Five9CredsSA.ini")
    Five9Username = config['Communications']['Five9UserName']
    logsomething("Five9 User Name: " + Five9Username)
    Five9Password = config['Communications']['Five9Password']
    logsomething("Five9 Password (don't tell anyone): " + Five9Password)


    My log file prints "Opened Five9CredsSA.ini" and then nothing more. The task ends with a result of 0x1. I have made sure that the "start in" folder in task scheduler points to where my code is; I have tried setting the task to run as admin (run with highest privileges) and also in user mode. I have re-installed Python 3.12 in "all users" mode (after removing the one in my appdata folder) and re-installed all the admin mode so that they exist for everyone. I must be missing something as I have searched the internet multiple times and all the recommendations don't seem to work for me. I believe it is a problem relating to configparser since that's where it stops running, but I don't see any errors in a log file that I tried to create. The batch file that I run is:

    echo Running Program > Five9INRRpt.log
    "C:/Program Files/Python312/python.exe" c:/Software/Py/Five9INRRpt.py >> Five9INRRpt.log


    And the output is this:

    Python program Five9INRRpt starting...
    2024-12-16 19:55:37 Reading the configuration files.
    2024-12-16 19:55:37 Loaded ConfigParser
    2024-12-16 19:55:40 Opened Five9CredsSA.ini


    So it is apparently able to open the configuration file but when it tries to read it it errors out. The computer that I'm running this on is running Windows 11 Enterprise as the Operating System.

    Thanks a bunch!

    Continue reading...

Compartilhe esta Página