LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Leveraging python in labview programs

Solved!
Go to solution

Hello all,

 

I figured out how to leverage python libraries in my labview program (e.g. numpy) using labview's built-in python nodes.  What I would like to do however, is now build this functionality into an application that can be reasonably installed on any computer and run using the labview runtime (so that I do not need the development version to run my labview program). 

 

Particularly, I am stuck on this step:

 

I need to install a downgraded 32-bit version of python, and pip install the desired modules/libraries (e.g., numpy).  Perhaps doing the pip installs in a virtual environment so that nothing needs to be installed/modified onto the base system.  Then I need my main labview VI that is making use of the python library to dynamically find the path to this version of python with my desired libraries in the correct Lib/site-packages folder.

 

I think I figured out how to build an installer so that it automatically launches the python installer of my choice, but I am not sure about the best way to proceed from there, with regard to pip installing the python modules and potentially doing all that in a venv.

 

Any suggestions/discussions are more than welcome, and much appreciated,

 

Thanks

0 Kudos
Message 1 of 10
(4,855 Views)

On the discussion side, why do you want to make a LabVIEW wrapper for numpy? Is it for the UI? On a side note there are ways to call python from LabVIEW that do not require a 32 bit version of python. 

______________________________________________________________
Have a pleasant day and be sure to learn Python for success and prosperity.
0 Kudos
Message 2 of 10
(4,840 Views)

Curious question, why?

If you're inclined to use Python, use it completely, or use LabVIEW completely? why mix up?

What is the specific goal you're trying to achieve?

Santhosh
Soliton Technologies

New to the forum? Please read community guidelines and how to ask smart questions

Only two ways to appreciate someone who spent their free time to reply/answer your question - give them Kudos or mark their reply as the answer/solution.

Finding it hard to source NI hardware? Try NI Trading Post
0 Kudos
Message 3 of 10
(4,838 Views)

It's not just for numpy, that was just an example.  My goal is to quickly improve (confusing and poorly written) labview programs that are already in use by my organization, using open-source python libraries (e.g., openCV, scipy, pandas, plotting libraries for prettier/richer plots/easily customizable) without having to either:

 

a. Rewrite the programs entirely in python

b. Learn significantly more LabView, which I prefer not to do since I don't think I'll ever get used to or appreciate the graphical language.  I am a little more comfortable with python than LabView, and I have limited time and a lot to learn, which I would rather invest into other programming languages/tasks.

 

Reading these forums, it seems like the "why?" response is pretty common.  Although I appreciate the discussion, it shouldn't really matter the reason for the question.  At the bare minimum I think it would be a cool thing to do... if its even possible.  I am sure some other people tried to do this before, but I can't find very good discussions on the topic.  I would just like to know if anyone developed a good workflow to integrate python into labview.  Working on it for a little while, it looks like it should be possible to do.

0 Kudos
Message 4 of 10
(4,813 Views)

The reason you see a lot of "Why" responses is that LabVIEW - Python bridge is the least travelled path (comparatively to C/.NET dll). Each of the programming languages has its own best traits and this bridge often poses a bottleneck, for instance, you can't pass objects.

 

If you're good at Python, you will find it best to implement any complex requirement.

Santhosh
Soliton Technologies

New to the forum? Please read community guidelines and how to ask smart questions

Only two ways to appreciate someone who spent their free time to reply/answer your question - give them Kudos or mark their reply as the answer/solution.

Finding it hard to source NI hardware? Try NI Trading Post
0 Kudos
Message 5 of 10
(4,799 Views)
Solution
Accepted by Luxomancer

@Luxomancer wrote:

It's not just for numpy, that was just an example.  My goal is to quickly improve (confusing and poorly written) labview programs that are already in use by my organization, using open-source python libraries (e.g., openCV, scipy, pandas, plotting libraries for prettier/richer plots/easily customizable) without having to either:

 

a. Rewrite the programs entirely in python

b. Learn significantly more LabView, which I prefer not to do since I don't think I'll ever get used to or appreciate the graphical language.  I am a little more comfortable with python than LabView, and I have limited time and a lot to learn, which I would rather invest into other programming languages/tasks.

 

Reading these forums, it seems like the "why?" response is pretty common.  Although I appreciate the discussion, it shouldn't really matter the reason for the question.  At the bare minimum I think it would be a cool thing to do... if its even possible.  I am sure some other people tried to do this before, but I can't find very good discussions on the topic.  I would just like to know if anyone developed a good workflow to integrate python into labview.  Working on it for a little while, it looks like it should be possible to do.


I ask why because I work quite a bit integrating testing systems with Python and LabVIEW and I have some experience integrating Python and LabView. So for me it's interesting to have a discussion about it. A direct path to solve your problem is to not use the built in support for Python in LabView. A simple solution is to call your Python program like this: 

 

https://forums.ni.com/t5/LabVIEW/Labview-Crash-when-call-keras-function-using-python-node/m-p/419670...

 

You can make an installer (in python or whatever you want, that you are comfortable with) that puts python on your PC then does all the PIP installs, sets the venv or interpreter in your PATH then call it (your code) using the above paradigm and it works great. This way there is no dependency in LabVIEW for a particular version of Python, you can use whatever version you want.  

 

Is this an acceptable solution?  If not, it's not the only solution. 

 

______________________________________________________________
Have a pleasant day and be sure to learn Python for success and prosperity.
Message 6 of 10
(4,750 Views)

Thanks Jay!  This is an acceptable solution indeed.

 

Thinking about it overnight, I did imagine that some form of command-line automation (for pip, venv, adding to path, etc) might be the way to do this, and that was my plan to move forward.

 

Then I saw your post, which if I am understanding you correctly, seems to confirm that.

0 Kudos
Message 7 of 10
(4,741 Views)

I am also interested in integrating Python with LabVIEW, and I agree that this is a useful discussion. I have developed an interface scheme but not gotten so far as figuring out how to deploy the application. Deployment is usually less of an issue for me since nearly all my software products are used only in-house. However, ease of deployment is quite important as there are many clients to maintain.

 

The “why not do it all in Python” question is silly, as there is quite a steep learning curve to developing [good] user interfaces and developing multithreaded applications in Python. Though I know several other languages, I’m a native G speaker. If LabVIEW does one (or two) things well, it’s effortless multithreading and easy data acquisition and graphing.

 

I am more interested in the integration of Python to add the ability to script behaviors and actions or to make use of some scientist’s algorithm without having to do it all again in LabVIEW. LabVIEW makes it easy to develop most of the procedural code of the application, but it does lack some flexibility when it comes to tweaking things R&D-style without requiring the developer to dig in and recompile everything.

 


@Jay14159265 wrote:

 

A direct path to solve your problem is to not use the built in support for Python in LabView. A simple solution is to call your Python program like this: 

 

https://forums.ni.com/t5/LabVIEW/Labview-Crash-when-call-keras-function-using-python-node/m-p/419670...


@Jay14159265, do you have any information on the performance implications of calling Python from the command line versus from within a session in LabVIEW? I can see why you would want to do this for the flexibility of the versions, but then why would NI invest in making the Python Node at all if this “older” approach was a better solution? Asked another way, what does the Python Node get you that the command line does not? Perhaps only easier argument/data passing?

_______________________________________________________________
"Computers are useless. They can only give you answers." - Pablo Picasso
Message 8 of 10
(4,731 Views)

@Luxomancer wrote:

Thanks Jay!  This is an acceptable solution indeed.

 

Thinking about it overnight, I did imagine that some form of command-line automation (for pip, venv, adding to path, etc) might be the way to do this, and that was my plan to move forward.

 

Then I saw your post, which if I am understanding you correctly, seems to confirm that.


Yes, you can call anything you want from the command line. 

 

Also, another trick is to use a post installer program to do all the setup. You can set a VI to run after install in the labview installer. You can then task this VI to do all the setup, even to the point of going out and grabbing the latest versions of stuff off a server then doing the install for all your packages. 

______________________________________________________________
Have a pleasant day and be sure to learn Python for success and prosperity.
Message 9 of 10
(4,727 Views)


@Jay14159265, do you have any information on the performance implications of calling Python from the command line versus from within a session in LabVIEW? I can see why you would want to do this for the flexibility of the versions, but then why would NI invest in making the Python Node at all if this “older” approach was a better solution? Asked another way, what does the Python Node get you that the command line does not? Perhaps only easier argument/data passing?


I will say that the method for calling python described in the previous post is the *easy* way to do it that was used before LabVIEW had python nodes. That being said, it is not the only way to do it. On the performance topic, when you call a python script directly, it has to launch the python interpreter then load all the modules then run the code. All this takes a long time so if you are worried about performance this is not a good way to call a simple python script that returns some value that you are expecting to use in your next bit of logic in your program. But if the python code you call continues to run in another process and you pass it data and it returns data, it can be as efficient as is possible, but it is  lot more involved in programming on the Python side. 

 

For the Python nodes in LabView it is stated: 

 

LabVIEW 2018 introduces the Python Node, which provides native capability to intuitively call a Python script from a LabVIEW Block Diagram. It uses LabVIEW primitives, providing a way to interoperate between the languages with low latency. 

 

I don't know how they implemented this but I would imagine LabVIEW launches a python interpreter in the background on the first call to a python node then for the next n calls, it just attaches to the running interpreter and runs the code. The advantage to this is that you can now easily call Python in LabView. the disadvantage is that you are required to use the LabView Python interpreter (2.7 or 3.6 last time I checked) that they set up in their own virtual environment. So using a LabView Python node can be fast, is easy to set up,  but is restrictive. Calling Python directly can be fast, is much harder to set up, but is not restrictive. So I would say that the Labview Python nodes are great if you have a *small* script that runs on 2.7 or 3.6 but once you start getting into large scripts that have more complicated dependencies you have to move out of the Python nodes to the direct call, unless you want to develop your Python code around the restrictions of the LabView Python node. 

 

more info: 

https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000015C6tSAE&l=en-US#:~:text=LabVIEW%20201....

 

______________________________________________________________
Have a pleasant day and be sure to learn Python for success and prosperity.
0 Kudos
Message 10 of 10
(4,718 Views)