Call Perl and Python Scripts from LabVIEW

Publish Date: Mar 15, 2016 | 7 Ratings | 2.86 out of 5 | Print | 3 Customer Reviews | Submit your review

Overview

The ability to call and re-use existing code can save a developer time and money. LabVIEW gives users the ability to directly access and integrate code written in a variety of languages. This article explains how to re-use test scripts developed in either the Perl or Python scripting languages using the System Exec VI in LabVIEW.

Table of Contents

  1. Calling the Script Interpreter
  2. Calling Perl and Python Scripts From the Command Line
  3. Calling Perl and Python Scripts from LabVIEW
  4. Using the Attached LabVIEW Code
  5. Related Links

Perl and Python are high-level programming languages that use an interpreter to translate the source code into an executable format at run time instead of being pre-compiled. An interpreter, such as ActivePerl or ActivePython, is necessary to execute this translation.

1. Calling the Script Interpreter

Calling the interpreter can be simplified by changing the Environment Path Variable to include the location where your interpreter and corresponding library files are saved. You can edit the Environment Path using the System Properties Editor. You will need to ensure that the Environmental Path Variable is modified to include the directory in which the Interpreter and corresponding library files are located in order for the machine to recognize the Perl and Python tokens as commands to use the interpreter.

  1. Right-click on My Computer  and select Properties
  2. Select the Advanced tab
  3. Click Environment Variables
  4. In System Variables, select the Path variable, and then click Edit
  5. Add the directory for where your Interpreter is saved

Note: This example was prepared using the Using the ActivePerl 5.10.0 Interpreter and the ActivePython  2.6.1 Interpreter, which can be downloaded from the APSN ActivePerl Website and ActiveState ActivePython Website, respectively. Both Installers have built in functionality to edit the Environmental Path Variable to include the directory location of the interpreter and corresponding library files.

Back to Top

2. Calling Perl and Python Scripts From the Command Line

  1. Select Start»Run
  2. Enter cmd to launch the command line
  3. Navigate to the directory where the Perl or Python script is saved
  4. To call the interpreter that will execute the script use the following command(s):

For Perl scripts, use:  'perl scriptName.pl argument' as shown in the image below.


For Python scripts, use:  'python  scriptName.py argument' as shown in the image below.

Back to Top

3. Calling Perl and Python Scripts from LabVIEW

In LabVIEW you can use the System Exec.vi to execute a system-level command line that can include any parameters supported by the application you want to launch. This VI can be used to call a command line argument that will launch the Perl or Python script. Ensuring that your Environment Path Variable includes the directory in which you Interpreter and corresponding library files are saved will allow any System Exec call to recognize the perl and python tokens as commands to use the Perl and Python Interpreters to execute the script.

Click here for further documentation on the System Exec VI

To call a Perl or Python script from LabVIEW you will need to pass in the following parameters to the System Exec.vi

  • command line: indicates the command LabVIEW calls to run a program. If the executable is not in a directory listed in the PATH environment variable, the command line must contain the full path to the executable.
  • working directory: the file system directory from which you want to execute the command.

Back to Top

4. Using the Attached LabVIEW Code

  1. Set-up your Environment Path Variable to include the directory in which your Interpreter is saved
  2. Save the TestScripts folder to your machine. This folder includes a Perl and Python script as well as LabVIEW code that is generated to call these scripts.
  3. Open the TestScripts Folder and launch the TestVI.vi by double-clicking on it.
  4. Configure the TestVI to include:
    1. Working directory: The directory where you have saved the TestScripts folder <Path>\TestScripts
    2. Command line: The command you will send to the command line
      1. PERL SCRIPTS:  perl MyScripts.pl argument
      2. PYTHON SCRIPTS:  python MyScript.py argument
  5. Click the Run arrow to execute the program 

Back to Top

5. Related Links

Back to Top

Customer Reviews
3 Reviews | Submit your review

Or you can try this https://forums.ni.com/t5/LabVIEW-APIs-Documents/Open-Python-Interface/ta-p/3612981  - Apr 13, 2017

Or you can try this https://forums.ni.com/t5/LabVIEW-APIs-Documents/Open-Python-Interface/ta-p/3612981

Good summary  - Jun 13, 2016

I'm very thankful for this article. I'd love to see more in-depth discussions, especially around Python.

Non-native  - Apr 5, 2010

This doesn't addresst native support as in Dspace or other systems.

Bookmark & Share


Downloads

Attachments:

test-scripts.zip


Ratings

Rate this document

Answered Your Question?
Yes No

Submit