Why Does Relative Paths in Python Change When Called from LabVIEW?

Updated Jan 18, 2019

Reported In

Software

  • LabVIEW

Programming Language

  • Python

Issue Details

I have a Python script that is called from LabVIEW using the Python Node. The Python script will generate a file path that is defined relative to the Python script directory using os.path.abspath(<relative path>). The generated file path seems fine when the script is called from the Windows Command Prompt. However, when called from LabVIEW the returned path is instead defined relative to C:\Windows\System32.

Why does this happen and why is there a difference?

Solution

The os.path.abspath(<relative path>) function in Python interprets the relative path as being relative to the current working directory (CWD). 

When calling the Python script from the Windows Command Promt, the CWD is normally the same as the Python script file's location. Consequently, the generated file paths defined relative to the CWD also becomes relative to the Python script file.

When the Python script is called using LabVIEW, the CWD is per default the directory from which LabVIEW has first been launched. I.e. if you open the LabVIEW from the Start Menu the CWD will be C:\Windows\System32, as observed.

There are two ways to approach this: 
  • Edit the Python script to refer to the script file location instead of the CWD when defining relative paths.
  • Programmatically change the CWD from LabVIEW.
There is an example VI that can be used as a subVI to get or set the CWD from LabVIEW. Thus if the CWD is set before calling the Python script, one can decide what directory the relative paths should be relative to. 

The example VI can be downloaded here: 
Get/Set Current Directory in LabVIEW

WAS THIS ARTICLE HELPFUL?

Not Helpful