How to call python script in UIVision

Hi im trying to call python script with xRunAndWait.
But it says [error] Disconnected. Any ideas?

{
  "Name": "test",
  "CreationDate": "2019-10-30",
  "Commands": [
        {
          "Command": "XRunAndWait",
          "Target": "python3",
          "Value": "/home/jyri/Desktop/test.py"
        },
        {
          "Command": "store",
          "Target": "${!xrun_exitcode}",
          "Value": "strng"
        },
        {
          "Command": "echo",
          "Target": "${strng}",
          "Value": ""
        }
      ]
    }


import sys

def read_file():
	with open("/home/jyri/Desktop/test.txt") as reader:
		strng = reader.readlines()
		print(strng)
	sys.exit(strng)


if __name__ == "__main__":
	read_file()

Excuse me, i’m going to use you thread to ask the same exact question.
I need to call a python script but it doesnt work.
I made a test script with output 5 random numbers:
3
5
7
1
6
i need those numbers to be writen to a file.csv so kantu can read those numbers by reading the csv file.
When i try the script on the command line all is ok
c:\python\scripts\testkantu.py > test.csv
The csv file is created with the 5 random numbers
But when i use xrun to call the script

{
“Command”: “XRun”,
“Target”: “c:\python\scripts\testkantu.py”,
“Value”: “> test.csv”
},

kantu finish without error but the file is not created, also the python script can use a seed to generate the numbers, like testkantu.py 5 if i want the seed to be a variable can i use
“Value”: “${seed} > test.csv”?

so the line is c:\python\scripts\testkantu.py 5 > test.csv

Thank you.

The old issue was a bug that has been fixed meanwhile.

A good XRun example is in this post: Storing image with consistent filename

can use a seed to generate the numbers, like testkantu.py 5 if i want the seed to be a variable can i use
“Value”: “${seed} > test.csv”?

Yes, that should work. As for why it does not work in your case, I will test it later today and update this post :wink:

I see the mistake in your code. You are calling the Python command line wrong. The syntax is C:\python27\python.exe Z:\yourcode\yourscript.py.

Example: The Python script takes the input, squares it and returns it via exit code:

Python script:

# XRUN Python V1.0

import sys

#get command line value
i = int (sys.argv[1])

#Square it!
i = i * i

# exit code in Python (limited to value between 0...255)
sys.exit(i)

Python RPA:

{
  "Name": "python test",
  "CreationDate": "2020-2-15",
  "Commands": [
    {
      "Command": "store",
      "Target": "5",
      "Value": "a"
    },
    {
      "Command": "XRunAndWait",
      "Target": "C:\\python27\\python.exe ",
      "Value": "c:\\test\\square.py ${a}"
    },
    {
      "Command": "echo",
      "Target": "exitcode=${!xrun_exitcode}",
      "Value": "green"
    }
  ]
}

Same for VBS here: Problem with xrun when running batch file with bat extension or converted exe file

Thank you for your help, but calling the python script is not my issue, i used the thread, but my problem is i need the output from the python script to be saved into a csv so i can process it later,

1 i get data from web
2 i process the data with python and get a csv
3 i read the csv and
4 do funny things.

The problem is in your example:

  {
      "Command": "XRunAndWait",
      "Target": "C:\\python27\\python.exe ",
      "Value": "c:\\test\\square.py ${a}"
    },

it should be:

  {
      "Command": "XRunAndWait",
      "Target": "C:\\python27\\python.exe ",
      "Value": "c:\\test\\square.py ${a} > file.csv"
    },

But the file.csv is not created