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: 

LabPython Global Variables

I have a piece of code, which runs in Python 2.5, but when called up from LabPython, fails with the following error:

Global Variable Error.PNG

 

Any ideas what may be causing this general type of error? I tried to include from wx import wx after the import statements, but this did not help either.

0 Kudos
Message 1 of 12
(5,429 Views)

Hi Shultz,

 

Does this happen all the time or intermittently?  Are you able to run other python scripts or do they all cause an error?

Scott A
SSP Product Manager
National Instruments
0 Kudos
Message 2 of 12
(5,399 Views)

I am able to run other python scripts through LabPython. I even wrote a simple GUI to test to compatability of wxPython, and it worked fine.This happens all the time with the script. The GUI does not even appear.

0 Kudos
Message 3 of 12
(5,397 Views)

Shultz,

 

It sounds like you are able to call wxPython from LabPython, is this the case?  When you created the simple test GUI did you use the "import wx" command?

Scott A
SSP Product Manager
National Instruments
0 Kudos
Message 4 of 12
(5,324 Views)

 


@Biscotti wrote:

Shultz,

 

It sounds like you are able to call wxPython from LabPython, is this the case?  When you created the simple test GUI did you use the "import wx" command?


Yes. I've used import wx and created a simple GUI using the wx commands.

 

0 Kudos
Message 5 of 12
(5,312 Views)

If the code works when calling it in python and it does not work when using LabPython then there is likely a function in there that is not supported by LabPython.  LabPython has not been updated since 2008 and is unfortunately not supported by National Instruments due to the third party and closed nature of its development.

 

I would suggest trying to call it as shown in Call Perl and Python Scripts from LabVIEW.

Scott A
SSP Product Manager
National Instruments
0 Kudos
Message 6 of 12
(5,300 Views)

Passing here and saying hi. I have apparently a similar problem, though using other modules, and a solution to it. It seems that labpython has (at least on LV2010sp1/Python 2.7.1 on which I'm testing) an issue with visibility of names, such that names defined at the top level are not visible inside functions. My workaround was to declare them global. In particular I had a script starting with

 

import numpy

 

...

 

def myfunction(...):

    # code using numpy functions

 

To have that work, I needed to declare global numpy after the import.

I think the problem has already been reported in the past (e.g. on OpenG forums, on lava.org forums) but I found nowhere such a simple solution...

I'm going to post about on labpython-users on sourceforge, maybe we could get some comment from Rolf.

 

Enrico

0 Kudos
Message 7 of 12
(5,270 Views)

Did we ever find a fix for this problem? I'm running labpython and as soon as I try to get a variable from the global namespace using global, labpython forgets about it, MOST of the time. for instance:

 

myVar = [1,2,3,4,5]
def foo():
	global myVar
	return

labpython throws an error in the execute VI saying it can't find myVar. The funny thing is this only happens maybe 9/10 times, if I put the execute VI in a while loop with the error cluster status value wired to the continue condition, the script will eventually execute without error.

I read that Rolf doesn't have financial incentive to fix this problem. I would like this to be fixed very much. So much, that I'd be willing to work on it myself, or pay whoever may be concerned to resolve this.

 

Screen Shot 2012-07-18 at 11.20.49 AM.png

0 Kudos
Message 8 of 12
(5,136 Views)

IIRC, myVar would have to be declared global both in the function and in the caller; at least I do that and my stuff works.

 

I do have issues instead, if I rapid-fire the execute node too fast. Are you sure that the fact that you're eventually getting out of the loop does mean that the script executed correctly?

 

Enrico

0 Kudos
Message 9 of 12
(5,124 Views)

Here is an illustrative example of the problem I am having. The problem I showed earlier was when a script was importing a module that used global variables, which is similar, but different to this case.

Here are the contents of junk.py:

global myGlobalList #declared global in global namespace, not used in foo(), doesn't show up on variables list
myGlobalList = [9,10,11]
myNum = 5 #not declared global, shows up on variables list
mylist = [1,2,3,4] #not declared global in the global namespace, but imported using global in foo()
myOtherList = [5,6,7,8] #not delcared global, not used in foo(), shows up.
def foo():
    myNum = 9999
    global mylist
    mylist.append(5)
foo()

mySum = 10 + myNum
mylist[2] += 1
myOtherList[2] += 1

 

Here is the block diagram

Screen Shot 2012-07-19 at 10.31.55 AM.png

here is the output:

Screen Shot 2012-07-19 at 10.34.02 AM.png

 

From this, we can see that any thing preceeded by global, labpython forgets about. Now, before you go and tell me I shouldn't be using global variables in the first place, (and I tend to agree with you) the real problem is that I am importing other people's modules that use global variables, and it would be infeasible to re-write them.

0 Kudos
Message 10 of 12
(5,108 Views)