Skip to main content

Call Method In Module

adminLess than 1 minute

Tips

source code in this tutorial

call_method_example.zip

Introduction

1. Target Directory on Python

The folder of the module(.py script) path is set as target directory. For example, in this tutorial project, the directory is like this:

โ””โ”€ call_method_sample
   โ”œโ”€ script_folder
   โ”‚  โ””โ”€ function.py
   โ””โ”€ module.py

/script_folder/function.py

import numpy as np

def square(x):
    print(np.square(x))

/module.py

print("call_method_sample")

from script_folder.function import *


import script_folder.function
def square_wrapper(x):
    script_folder.function.square(x)

Let's import "module.py" in LabVIEW to call square function. This example shows two ways: directly import the function in "script_folder.function" then call "square", or rewrite the square function in "module.py" as the name as "square_wrapper" then call "square_wrapper".

2. Call Python Method via LabVIEW

First, wire the "module.py" path to the "Module Path(.py)" terminal in Create Session.vi. After importing this script, the print function (print("call_method_sample")) will be executed.

Then, use Add Argument.vi to give the argument of the square function and use Call Py Method.vi to call the function name.

Run this VI attached in the source code file and see result on python console.

Last update:
Contributors: ZHUO DIAO