02-20-2019 04:45 PM
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
Solved! Go to Solution.
02-21-2019 05:16 PM
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?
02-22-2019 09:31 AM
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.
05-05-2020 02:12 PM
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 🙂
10-20-2022 02:55 PM
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