Python Forum

Full Version: Call a python file with anothr python file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have this code that pings a PLC to se if it is online if it is on it will start the PLC file that
corresponds to the IP, it works but when the PLC fiel ends it closes all
instead of going back to the code that validates the IP

whats wront with the code?

import subprocess
import sys
import os

hostname = '172.21.12.10'
response = os.system("ping " + hostname)


if response == 0:
    print (hostname, 'PLC EN LINEA, INICIANDO REVISION')
    exec(open("SE1_F1.py").read())
  
else:
    print ('SE1_F1 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
    hostname = ' 192.168.100.5'
    response = os.system("ping " + hostname)
  
    if response == 0:
        print ('SE1_F2 EN LINEA, INICIANDO REVISION')
        exec(open("SE1_F2.py").read())
          
    else:
        print ('SE1_F2 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
        hostname = '172.21.13.10'
        response = os.system("ping " + hostname)

        if response == 0:
            print('SE2_R1 EN LINEA, INCIANDO REVISION')
            exec(open("SE2_R1.py").read())
        else:
            print ('SE2_R1 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
            hostname = '172.21.13.12'
            response = os.system("ping " + hostname)

            if response == 0:
                print('SE2_R2 EN LINEA, INCIANDO REVISION')
                exec(open("SE2_R2.py").read())
            else:
                print ('SE2_R2 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
                hostname = '172.21.14.10'
                response = os.system("ping " + hostname)

                if response == 0:
                    print('SE3_M1 EN LINEA, INCIANDO REVISION')
                    exec(open("SE3_M1.py").read())
                else:
                    print ('SE3_M1 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
                    hostname = '172.21.14.12'
                    response = os.system("ping " + hostname)

                    if response == 0:
                        print('SE3_M2 EN LINEA, INCIANDO REVISION')
                        exec(open("SE3_M2.py").read())
                    else:
                        print ('SE3_M2 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
                        hostname = '172.21.11.10'
                        response = os.system("ping " + hostname)

                        if response == 0:
                            print('SE4_S1 EN LINEA, INCIANDO REVISION')
                            exec(open("SE4_S1.py").read())
                        else:
                            print ('SE4_S1 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
                            hostname = '172.21.11.11'
                            response = os.system("ping " + hostname)

                            if response == 0:
                                print('SE4_S2 EN LINEA, INCIANDO REVISION')
                                exec(open("SE4_S2.py").read())
                            else:
                                print("NINGUN PLC EN LINEA")
the SE1_F2 is to loong to show it here
but i can show you the SE1_F2 (that is the one that is called if SE1_F2 is not responding)

from pylogix import PLC
import sys
sys.path.append('..')
import time
import pandas as pd

plc_data = pd.read_excel("Actuadores.xlsx", sheet_name='Result')
with PLC() as comm:
    comm.IPAddress = '172.21.12.12'
    comm.ProcessorSlot = 0
    ret = comm.GetPLCTime()
    print( "Fecha y Hora de Registro Revision Fallas", ret.Value)
    time.sleep(1)
    Estacion = "D2M_150"
    Estacion2 = "D2R_155"
#------------------------------------------------------------------------------------------ ESTACION D2M_150 -------------------------------------------------------------------------------#
    print("INICIO DE REVISION ESTACION :" , Estacion)
#ACTUADOR 1 
    ACTUADOR = comm.Read('D2M_150.Status.Act._1.WBypassed')
    ret = comm.GetPLCTime()
    Numero_Act_EnBypass = 0
    
    if ACTUADOR.Value == True:
        Sensor = "ACTUADOR 1 WORK SENSOR EN BY PASS"
        Numero_Act_EnBypass= (Numero_Act_EnBypass+1)
        temp_dict = {'PLC IP': comm.IPAddress, 'Date Time': ret.Value, 'Estacion': Estacion, 'Tag': ACTUADOR, 'Value': Sensor}
        plc_data = plc_data.append(temp_dict.copy(), ignore_index=True)
        plc_data.to_excel("Actuadores.xlsx", sheet_name='Result', index=False) 
        print(Sensor)

    else:
        Sensor = "ACTUADOR 1 WORK OK" 
        print(Sensor)
   


    ACTUADOR = comm.Read('D2M_150.Status.Act._1.HBypassed')
    ret = comm.GetPLCTime()
    if ACTUADOR.Value == True:
        Sensor = "ACTUADOR 1 HOME SENSOR EN BY PASS"
        Numero_Act_EnBypass= (Numero_Act_EnBypass+1)
        temp_dict = {'PLC IP': comm.IPAddress, 'Date Time': ret.Value, 'Estacion': Estacion, 'Tag': ACTUADOR, 'Value': Sensor}
        plc_data = plc_data.append(temp_dict.copy(), ignore_index=True)
        plc_data.to_excel("Actuadores.xlsx", sheet_name='Result', index=False) 
        print(Sensor)

    else:
        Sensor = "ACTUADOR 1 HOME OK"
        print(Sensor)
#ACTUADOR 2 
    ACTUADOR = comm.Read('D2M_150.Status.Act._2.WBypassed')
    ret = comm.GetPLCTime()
    
    if ACTUADOR.Value == True:
        Sensor = "ACTUADOR 2 WORK SENSOR EN BY PASS"
        Numero_Act_EnBypass= (Numero_Act_EnBypass+1)
        temp_dict = {'PLC IP': comm.IPAddress, 'Date Time': ret.Value, 'Estacion': Estacion, 'Tag': ACTUADOR, 'Value': Sensor}
        plc_data = plc_data.append(temp_dict.copy(), ignore_index=True)
        plc_data.to_excel("Actuadores.xlsx", sheet_name='Result', index=False)
        print(Sensor)

    else:
        Sensor = "ACTUADOR 2 WORK OK" 
        print(Sensor)
   


    ACTUADOR = comm.Read('D2M_150.Status.Act._2.HBypassed')
    ret = comm.GetPLCTime()
    if ACTUADOR.Value == True:
        Sensor = "ACTUADOR 2 HOME SENSOR EN BY PASS"
        Numero_Act_EnBypass= (Numero_Act_EnBypass+1)
        temp_dict = {'PLC IP': comm.IPAddress, 'Date Time': ret.Value, 'Estacion': Estacion, 'Tag': ACTUADOR, 'Value': Sensor}
        plc_data = plc_data.append(temp_dict.copy(), ignore_index=True)
        plc_data.to_excel("Actuadores.xlsx", sheet_name='Result', index=False)
        print(Sensor)

    else:
        Sensor = "ACTUADOR 2 HOME OK" 
        print(Sensor)

    print("Terminado")
    print("Actuadores en Bypass:", Numero_Act_EnBypass)
    comm._closeConnection
I don't know. What is it doing wrong? Does it report an error? Does it not do what it is supposed to do? More details please.
It has no errores

the problem is that when it finish the SE1_F2 (or any other) it closes all
and i need that when the script finish, go back to the main script and validate the next IP
The reason I ask is because your program is written to stop running after the first successful connection.
If I can talk to PLC_1
    configure PLC_1
else
    If I can talk to PLC_2
        configure PLC_2
You can only get to PLC_2 if the connection to PLC_1 fails. Instead you should call them like this.
If I can connect to PLC_1
    configure PLC_1
If I can connect to PLC_2
    configure PLC_2