How do I send data from LabView to Python and get a result back?
9 Answers
One other solution is using the smart messaging library ZeroMQ, which comes with a lot of bindings, almost for all major languages.
For the Python/Labview case there is a nice demo project on sourceforge:
Client-side ~LabVIEW
+
Server-side part (example)
#-----------------------------------------# INFRASTRUCTURE for communication
context = zmq.Context() # I/O-DAEMON CONTEXT
socket = context.socket(zmq.REP) # ARCHETYPE for a Smart Messaging
socket.bind( "tcp://127.0.0.1:5555" ) # PORT ready for LabView to .connect()
#-----------------------------------------# TRANSPORT-CLASS-es {ipc|tcp|+..}
while True: # LOOP & WAIT FOR REQ-calls
# # Wait for request from client
message = socket.recv()
print("Received request: %s" % message )
try:
r = eval( message )
print(r )
socket.send(bytearray(str( r ),
'utf-8' )) # send returned value as bytearry to client
except NameError:
socket.send( b"Unknown command" )
except:
socket.send( b"Unknown error" )

- 2,154
- 1
- 16
- 36
-
1While I do understand your criticism, the message of this answer is "There is a library which does what you want and here is an tutorial implementation". Which exactly is the context you demand. I rephrased my answer to better point this out. If you find this answer to not meet the standards of this site, I will gladly delete my answer. But I searched hours before I found this library and it really saved my day. I think it is worth to point out it existence to people asking this question. – user_na Sep 26 '16 at 09:17
-
-
@DakotaD for me it also did not work. I recreated the snippet and now it works for me. Please try again. – user_na Nov 22 '17 at 14:29
LabView allows you to write extensions in several languages, the primary technique these days is to use a network connection. Native language toolkits that run inside the labview process itself are avoided.
It seems there is a Python Labview toolkit here but it no longer works.
Use a socket server and socket client to talk between Labview and python. (Most cross-platform solution, and now you don't have to have your python and labview running on the same PC, or in the same process).
Unfortunately a sample is beyond me at the moment as I don't have labview installed, but I have done ole automation based integrations from LabView to dozens of apps in two or three languages, but that was many years ago. These days I would use the network socket technique.

- 65,725
- 40
- 181
- 316
-
LabPython is outdated and does not work for even basic scripts anymore. The socket approach is the accepted one in the community, and is actually how most large LabVIEW projects interface with other languages. – ijustlovemath Oct 03 '16 at 20:16
-
This answer was from 5 years ago. Even five years ago I would have avoided custom code if I could. – Warren P Oct 04 '16 at 00:43
-
Sorry, didn't notice the date. Being a Q&A site, you may want to update your answer to reflect LabPython being defunct. – ijustlovemath Oct 04 '16 at 01:36
-
Normallly you should just edit it yourself. That's how it works. Imagine I was no longer active on stackoverflow (which will be the case for 90% of 5 year old posts on SO). I updated my answer because I happen to not be dead, but next time, just click that little "edit" up there and fix it yourself. – Warren P Oct 04 '16 at 11:13
LabVIEW 2018 now offers a "native" solution to calling Python code from LabVIEW with "sending data" back and forth:
The Connectivity palette includes the new Python subpalette, which you can use to call Python code from LabVIEW code. The Python palette includes the following functions:
- Open Python Session — Opens a Python session with a specific version of Python.
- Python Node — Calls a Python function directly.
Close Python Session — Closes a Python session.
Note You must install Python 2.7 or 3.6 to use the LabVIEW Python functions. Although unsupported versions might work with the LabVIEW Python functions, NI recommends using supported versions of Python only.
Reference: LabVIEW 2018 Features and Changes
The Python Node is provided with the module path, function name, input parameters, and the expected data type of the return value. These inputs and output support a number of basic data types: numerics, (multi-dimensional) arrays, strings, clusters; with automatic conversion to corresponding data types, i.e. LabVIEW arrays are converted to Python lists, and clusters to tuples (and vice versa for the return value).

- 409
- 10
- 24
Server side with Python
import socket
server = socket.socket(2,1)
server.bind(('localhost',2000))
server.listen(1)
while True :
(conn,addr) = server.accept()
command = conn.recv(4)
print (command)
if 'INIT' in str(command):
conn.sendall(b'INIT-DONE')
elif 'PLAY' in str(command):
conn.sendall(b'PLAY-DONE')
elif 'QUIT' in str(command):
conn.sendall(b'QUIT-DONE')
break
server.close()

- 139
- 1
- 4
There is a new Python/LabVIEW connector out, built mostly by yours truly, called TestScript. It's a free, source-released Python/LabVIEW connector that is fully bidirectional. You can control LabVIEW from within a Python script, and you can call Python scripts from LabVIEW. It ships with several examples illustrating how you can send data from LabVIEW to Python and get a result back. In particular, the Simple Scripting Example - Add on Python Side.vi shows how TestScript accomplishes what you need.
Enjoy!

- 842
- 3
- 15
- 33
-
TestScript is really nice. How many devs maintain this piece of code? – abunickabhi Nov 03 '21 at 11:11
-
1@abunickabhi When I was developing it, I was the sole maintainer. We pounded on it for months to get all the bugs out and improve performance. Eventually I left Wineman Technology, which has since joined Genuen. I don't know how many people maintain it, now. Thanks for the compliment! The idea for TestScript was my supervisor's; I was the one to implement it. I do think it's got a lot going for it - more than the NI Python node or other options: TestScript is fully bidirectional in every way, which is unique. – Adrian Keister Nov 03 '21 at 13:29
Python-LabVIEW-Interface (PyLVi) is an open-source project based on the ZeroMQ library to call Python functions and read the results back to LabVIEW.
It supports call of class methods and properties, and can handle structured datatypes (e.g. Python dictionaries are mapped to LabVIEW clusters) as well as numpy arrays.

- 31
- 1
I was using stdio communication with a Python process for a while and recently noticed Python for .Net ( http://pythonnet.github.io/ ) which works for me.
Just copy the .dll in your LabVIEW project, create .Net constructors, and using the LabVIEW .Net you can quickly figure out how to use this library, which basically provides you with the unmanaged Python shared library, wrapped with .Net goodness.

- 5,610
- 13
- 81
- 138

- 1,484
- 10
- 19
You can try this: https://forums.ni.com/t5/LabVIEW-APIs-Documents/Open-Python-Interface/ta-p/3612981
Its an Open Python Interface toolkit for LabVIEW.

- 171
- 2
- 6
Here is a good quick solution but kind of unelegant. Just use a text file that both labview and python can read/write from. Labview writes commands to text file and python waits until a command is read or vice versa. you can have a read text file and a write text file at the same time. However this is a slower solution as it takes time to read and write.
-
That's a horrible idea. I say that as a person who has cleaned up crap like this in LabVIEW codebases in the past. – Warren P Sep 26 '16 at 12:59