NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Python script (some) called from TS does not work

Solved!
Go to solution

Calling the attached .py script from teststand does not work (exit code 1)

Executing directly - it works !

Removing the  line "f = open('debug2.txt', 'w')" - makes it work also from Teststand (exit code 0).

 

I have other more complicated script which also works when executing directly, but not from teststand (exit code 1).

 

What is going on ?

 

 

 

import sys

f = open('debug2.txt', 'w')

#Command Line Arguements are stored in list argv
numArgs = len(sys.argv) - 1
sum = 0

#Iterate through each element and add to the sum
for n in range (1, len(sys.argv)):
    sum = sum + int(sys.argv[n])

print("Sum:", sum, "\n")

 

0 Kudos
Message 1 of 4
(3,442 Views)

I guess that you working directory is not the directory where the Python script is located when you run from teststand. When I run a batch file from teststand, one of the first commands that are executed is PUSHD "%~dp0"  to switch to the batch file folder as working directory, so the other files in this directory can be used without absolute paths. I don't know how to do this in Python.

0 Kudos
Message 2 of 4
(3,402 Views)

Hello again!

 

Exit code 1 indicates a Command-line Error. Are you running it by setting the file pathname in 'call settings' as the location of python, and then putting the argument expression 'AddNumbersCommandLine.py'? And what are you setting the working directory as? Svh's suggestion sounds like a good one - is debug2.txt in the same folder as your sequence file?

 

When I run it from TestStand, it works and I get the correct output, see screenshot for how I did it.

 

Let me know if this works!

 

Best regards,

 

Leah

0 Kudos
Message 3 of 4
(3,396 Views)
Solution
Accepted by topic author NikolajEgeskovOstergaard

It is in fact related to "working directory". I've been struggling with that before with teststand.

 

when hardcoding the path

or changing

"os.getcwd()"

to

"os.path.dirname(os.path.realpath(sys.argv[0]))"

 

my python scripts are also working from Teststand

0 Kudos
Message 4 of 4
(3,393 Views)