From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Python Step // Cannot run python script from its current location

Hi all,

I need to run a python script from a sequence using the python step found here.

My sequence looks like this:

sequence.png

Both TS-DummyGateway.py and TS-OtherDummyGateway.py contain the following code:

import sys
import datetime
import os

def hello():
	return "Hello, now is : "  + str(datetime.datetime.now()) + "& current dir :" + str(os.getcwd());

The sequence calls both python script and display their return messages.

The python step setup:

pythonStepConfig.png

Both steps have the same config besides the python file path.

 

When running the sequence I am expecting the python script to display its current location (ie the location of the .py file), instead of that I am getting the folder where the sequence is located. In order to fix it I just need to config the step and select again the python script that is in the field "file path". When I then run the sequence again I can this time see the proper python script location being displayed on screen.

If I close TestStand and re-open it will misbehave again and I have to do that trick (select the script that is already picked in "file path") to make it work.

Finally, when I fix one of the python steps, both will display the folder of the one that just got fixed, it is not possible for them to display separate folders.

 

My interpretation: when TestStand is opened it selects by default its surrounding folder as environement, when I select a python script this environement changes to the folder containing the script, and so on for each script changes.

 

What I would like: for python to run from where it is located.

 

Has anyone any idea on how to do that? I cannot be the only that has come across this problem.

 

Thanks in advance for any tip ! 🙂

 

Attached is the Sequence and both python files, you will need to put them wherever you like in your file system and configure the sequence accordingly.

Download All
0 Kudos
Message 1 of 3
(2,192 Views)
os.getcwd() will not give you the path where the script is located rather it will give the path from where the script is executed.
os.getcwd() is typically the current working directory of the process and it can be changed by any code executed in that process.
 
In python, you can use os.path.dirname(os.path.abspath(__file__)) to get the directory of the script and os.chdir() to change the path of current working directory.
 
So, use the following line in your script to modify the current working directory of the process to the directory where your script is located:
os.chdir(os.path.dirname(os.path.abspath(__file__)))
 
-Shashidhar
Message 2 of 3
(2,171 Views)

Hi Shashidhar,

 

That's what I tried at first using a hard coded value for the file path but it didn't work, I don't know why.

I then tried what you proposed and it worked perfectly ! Thanks for your help.

0 Kudos
Message 3 of 3
(2,149 Views)