Invoke Python Method: Doesnt work

Hi guys, I have a python script that executes a request to a webservice and receives the response back in xml format. When running the script through cmd, it works correctly.
But when trying to run it through UiPath, I can’t. Below print and description of what I did:

  1. In Python Scope I have the Library Path field and the Path field, I filled them with the following values:
    Library Path: “C:\Users\username\AppData\Local\Programs\Python\Python38\python38.dll”
    Path:"C:\Users\username\AppData\Local\Programs\Python\Python38"

  2. In Load Python Script, I filled in the path fields for the .py file and the output with a variable.

  1. Finally, I called the Invoke Python Method, calling my “get_boleto” function from the script. PS: In my InputParameters I have the variable “numProcessamento” which is of type IEnumerable, and I needed to assign a value of type string within the InputParameter, so I created the string and passed it to “numProcessamento” using New List( Of Object) From {numProcessamento_str}. And when I try to run it, the Invoke Python Method gives an error.

Here is my .py script and my numProcessamento value for test is this “117009180”:

import sys
import requests
import json

def get_boleto(numero_processamento):
    soap_request = f"""
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://servicos.wsdetran.com.br">
       <soapenv:Header/>
       <soapenv:Body>
          <ser:consPagamentoDarUnico>
             <ser:in0>
             <![CDATA[<?xml version="1.0" encoding="UTF-8"?>
             <TEnvio_ConsultPagamentoUnico xsi:schemaLocation="http://www.sefaz.al.gov.br Envio_ConsultaPagamento_v1.00.xsd" xmlns="http://www.sefaz.al.gov.br" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                 <DadosConsultaUnico> 
                   <numeroProcessamento>{numero_processamento}</numeroProcessamento>
                 </DadosConsultaUnico>
                 <tipoConsulta>N</tipoConsulta>
             </TEnvio_ConsultPagamentoUnico>
             ]]>
             </ser:in0>
          </ser:consPagamentoDarUnico>
       </soapenv:Body>
    </soapenv:Envelope>
    """

    headers = {
        'Content-Type': 'text/xml',
        'SOAPAction': 'http://servicos.wsdetran.com.br/consPagamentoDarUnico',
    }

    response = requests.post('http://emissaodarws.sefaz.al.gov.br:80/WSEmissaoDar/services', headers=headers, data=soap_request)

    if response.status_code == 200:
        print("Resposta da solicitação SOAP:")
        xml_result = response.content.decode('utf-8')
        #print(xml_result)  # Print da resposta para depuração
        return xml_result
    else:
        print('Error making SOAP request')
        return {'error': 'Error making SOAP request'}
		
if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Uso: python get_boleto.py <numero_processamento>")
        sys.exit(1)

    numero_processamento = sys.argv[1]  # Recebe o argumento externo
    resultado = get_boleto(numero_processamento)
    print(resultado)

Thanks!! :smiley:

Hi @sheCode

can you share the error screenshot how you getting the error.

yeah, look:


It’s a simple error with no description…

also, here is my .py script and my numProcessamento value for test is this “117009180”:

import sys
import requests
import json

def get_boleto(numero_processamento):
    soap_request = f"""
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://servicos.wsdetran.com.br">
       <soapenv:Header/>
       <soapenv:Body>
          <ser:consPagamentoDarUnico>
             <ser:in0>
             <![CDATA[<?xml version="1.0" encoding="UTF-8"?>
             <TEnvio_ConsultPagamentoUnico xsi:schemaLocation="http://www.sefaz.al.gov.br Envio_ConsultaPagamento_v1.00.xsd" xmlns="http://www.sefaz.al.gov.br" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                 <DadosConsultaUnico> 
                   <numeroProcessamento>{numero_processamento}</numeroProcessamento>
                 </DadosConsultaUnico>
                 <tipoConsulta>N</tipoConsulta>
             </TEnvio_ConsultPagamentoUnico>
             ]]>
             </ser:in0>
          </ser:consPagamentoDarUnico>
       </soapenv:Body>
    </soapenv:Envelope>
    """

    headers = {
        'Content-Type': 'text/xml',
        'SOAPAction': 'http://servicos.wsdetran.com.br/consPagamentoDarUnico',
    }

    response = requests.post('http://emissaodarws.sefaz.al.gov.br:80/WSEmissaoDar/services', headers=headers, data=soap_request)

    if response.status_code == 200:
        print("Resposta da solicitação SOAP:")
        xml_result = response.content.decode('utf-8')
        #print(xml_result)  # Print da resposta para depuração
        return xml_result
    else:
        print('Error making SOAP request')
        return {'error': 'Error making SOAP request'}
		
if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Uso: python get_boleto.py <numero_processamento>")
        sys.exit(1)

    numero_processamento = sys.argv[1]  # Recebe o argumento externo
    resultado = get_boleto(numero_processamento)
    print(resultado)

can you try to surround your code with try except exception handler method in python.

starting code with try

at the end use except Exception as e:

the you can use print(e0 to print the exception.

atleast, you can come to know what kind of exception it is.

@sheCode,

You missed to pass Instance property of Invoke Python Method. It should be Result variable of Load Python Script

Instance = testPyResult

Thanks,
Ashok :slight_smile:

@sheCode

Try to open the error description from locals panel

cheers

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.