LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Using python in loop without importing numpy each time

Solved!
Go to solution

Hi,

Sorry for the inelegant title but it is accurate. I'm using a python function in a labview loop. I'm using numpy in this function and thus need to import it.

 

Do I have the import it a each loop? Isn't there a way I can import numpy once and keep using the same python session while labview update new inputs for my function. I've tried to link one python node to the other but it doesn't seem to work. I've linked my example where I import each time.

 

Thank you

RMT

Download All
0 Kudos
Message 1 of 5
(5,067 Views)

Hey RaphaelMT,

 

To my knowledge, there is not currently a way to import something permanently because LabVIEW executes separate Python code each iteration of the loop. You could try using two Python Nodes. The first one outside of the loop could just include the code to import numpy for the session and the second Python Node could execute your desired functionality within the loop. I can't guarantee this will work, but it's worth a try.

 

I'm interested in what you are trying to do with Python in your end application? It may be easier to perform through other methods within LabVIEW.

 

Also, have you looked into the Python Integration Toolkit for LabVIEW?

 

Ryan B.
Technical Support Engineer
National Instruments
Message 2 of 5
(5,019 Views)

That was the first thing I tried. I haven't check the toolkit though. It looks like I'll run in the same problem but I'll test it.

 

Thanks.

0 Kudos
Message 3 of 5
(5,000 Views)
Solution
Accepted by topic author RaphaelMT

I was trying to do the same thing. It is kind of intuitive. In your python script make a function that imports all needed libraries and declares them globally. It can be done using the keyword global. So to import numpy, you would do this :

def importing():

    global np

    import numpy as np

    return True

 

Now in Labview make a session outside the loop. Feed it into the python node and call the function importing(). Feed the session out into the python node inside the loop. And there you have it 🙂

0 Kudos
Message 4 of 5
(4,532 Views)

I have a related question.  What exactly is meant by a Python session?  Say a Python node calls one module with some function definitions and then I pass the session to another node with some new definitions, but these new definitions call functions from the first module.  This results in an error.  In Python, obviously, this doesn't happen but in LabView, it seems like Python doesn't remember what has been defined once you go outside the node even though the same session is being used.  I think I can use the idea you suggest here, which is to just create one module with all the function definitions, but, to avoid having to import the libraries each time, I can declare them as 'global'.  I'm just wondering if I'm doing something wrong.

 

Thanks

 

0 Kudos
Message 5 of 5
(2,953 Views)