From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

python code not working when called through labview, does work when called outside of labview

I have a Python routine (3.7) that I use to capture a picture with an 10 GigE camera.  The routine does all of the camera configuration operations, takes the picture and then converts the picture into a 2D array of 16 bit integers which it returns.  If I run the Python routine outside of LabVIEW, it's fine. If I call it directly from LabVIEW, the camera isn't found and subsequently no picture is taken etc..  If I comment out all of the camera specific code in the Python routine and simulate the picture with a 2D array of random numbers, the routine works when called from LabVIEW.  Any ideas why the code when called from LabVIEW doesn't work but when called directly does?  I've attached a image of the code.  There's nothing to it.

0 Kudos
Message 1 of 8
(2,269 Views)

It probably has to do with the paths in the python code being wrong when called by the LabVIEW program.  (Can't be sure you didn't show that part!) 

 

If the dependencies in the python code are suddenly looking in the path of the LabVIEW caller then they are probably wrong and your python code fails.  Add some logging in your python code to note where it thinks its located when called normally and from the LabVIEW vi, then adjust paths accordingly.  

0 Kudos
Message 2 of 8
(2,200 Views)

It doesn't appear that the paths are the problem.  When running from labview, labview adds to the paths but the other entries are consistent with those running outside of labview.  However, it appears that when launching the Python code from labview, the python code is executed twice. The first time the Python code runs, it appears to be fine.  The second time it fails to find the camera.  I can begin to understand that if the camera hasn't had time to reinitialize after the first call, but why is it being called twice?

 

The "debug" information I added to the Python code generate the text log below.  Why is it running twice?

 

just started
system paths D:\Users\jg\Temp
system paths C:\Program Files\National Instruments\Shared\NIPythonInterface\.\
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37\python37.zip
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37\Lib
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37\DLLs
system paths C:\Program Files\National Instruments\Shared\NIPythonInterface
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37\lib\site-packages
just got system
just got camera list
just got number of cameras 1
about to begin acquisition
about to get image
just got image
getting image data
just got image data, rows 200 cols 2000
just about to save the image data
just saved the image data
just copied image data
about to release image
just started
system paths D:\Users\jg\Temp
system paths C:\Program Files\National Instruments\Shared\NIPythonInterface\.\
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37\python37.zip
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37\Lib
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37\DLLs
system paths C:\Program Files\National Instruments\Shared\NIPythonInterface
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37\lib\site-packages
just got system
just got camera list
just got number of cameras 0

0 Kudos
Message 3 of 8
(2,182 Views)

I am calling a Python script (3.7) from LabVIEW that is used to capture an image from a 10GigE camera.  They Python script creates a debug text file that I've been using to try to figure out why it isn't working when called from LabVIEW, but is working when called outside of LabVIEW.  It appears that the Python code is being called twice.  The log file is below, and I've attached an image of the LabVIEW code.  There's nothing to it.  Just call the script and end the session.  If you look at the debug file, you can see that the Python code is executed twice.  The first time everything is fine.  The second time it fails.  The failure on the second execution may make sense as the camera doesn't have time to clear and reinitialize between calls.   Any ideas?  Does LabVIEW launch the Python routine in some sort of behind the scenes loop?  Do I need to clear some sort of buffer that has been flooded with calls to run the script?

 

just started
system paths D:\Users\jg\Temp
system paths C:\Program Files\National Instruments\Shared\NIPythonInterface\.\
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37\python37.zip
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37\Lib
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37\DLLs
system paths C:\Program Files\National Instruments\Shared\NIPythonInterface
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37\lib\site-packages
just got system
just got camera list
just got number of cameras 1
about to begin acquisition
about to get image
just got image
getting image data
just got image data, rows 200 cols 2000
just about to save the image data
just saved the image data
just copied image data
about to release image
just started  ??????? Why a second time?
system paths D:\Users\jg\Temp
system paths C:\Program Files\National Instruments\Shared\NIPythonInterface\.\
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37\python37.zip
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37\Lib
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37\DLLs
system paths C:\Program Files\National Instruments\Shared\NIPythonInterface
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37
system paths D:\Users\jg\AppData\Local\Programs\Python\Python37\lib\site-packages
just got system
just got camera list
just got number of cameras 0

0 Kudos
Message 4 of 8
(2,192 Views)

I'd be initially suspicious about the Local Variables (especially for the session).

 

Is this just simplified for the forum, or is that really the code you're calling? If so, can you show the full VI rather than a small section in an image?


GCentral
0 Kudos
Message 5 of 8
(2,180 Views)

I'm guessing the code is in a loop with Local Variables? If so, welcome to Race Conditions.

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 6 of 8
(2,170 Views)

Is that picture of the BD all of your code or part of a bigger program?  Is that boolean a button?  How is it configured?  Does it trigger twice?

0 Kudos
Message 7 of 8
(2,171 Views)

I have a very similar problem reaching my device over python by Labview. Any help would be appreciated.

 

https://forums.ni.com/t5/LabVIEW/Labview-Python-integration-doesn-t-work-even-though-there-is-no/m-p...

0 Kudos
Message 8 of 8
(188 Views)