Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28571 > unrolled thread

Need help fixing this error please:NameError: global name is not defined

Started byshaun <shaun.wiseman91@gmail.com>
First post2012-09-06 03:45 -0700
Last post2012-09-06 17:16 -0400
Articles 9 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Need help fixing this error please:NameError: global name is not defined shaun <shaun.wiseman91@gmail.com> - 2012-09-06 03:45 -0700
    Re: Need help fixing this error please:NameError: global name is not defined Chris Angelico <rosuav@gmail.com> - 2012-09-06 21:05 +1000
    Re: Need help fixing this error please:NameError: global name is not defined Dave Angel <d@davea.name> - 2012-09-06 07:05 -0400
    Re: Need help fixing this error please:NameError: global name is not defined Peter Otten <__peter__@web.de> - 2012-09-06 13:07 +0200
    Re: Need help fixing this error please:NameError: global name is not defined Chris Angelico <rosuav@gmail.com> - 2012-09-06 21:53 +1000
    Re: Need help fixing this error please:NameError: global name is not defined shaun <shaun.wiseman91@gmail.com> - 2012-09-06 05:07 -0700
      Re: Need help fixing this error please:NameError: global name is not defined Chris Angelico <rosuav@gmail.com> - 2012-09-06 23:16 +1000
    Re: Need help fixing this error please:NameError: global name is not defined MRAB <python@mrabarnett.plus.com> - 2012-09-06 13:08 +0100
    Re: Need help fixing this error please:NameError: global name is not defined Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-06 17:16 -0400

#28571 — Need help fixing this error please:NameError: global name is not defined

Fromshaun <shaun.wiseman91@gmail.com>
Date2012-09-06 03:45 -0700
SubjectNeed help fixing this error please:NameError: global name is not defined
Message-ID<149e9472-ec31-4b74-9f20-d4945a9fb678@googlegroups.com>
Hi all,

 I have a class which I create an object from in a different script but when its run I get an error at the last part of this method:

////CODE///////////////////////////////

def databasebatchcall(self,tid, bid):
        con=cx_Oracle.connect('user/user123@odb4.dcc.company/ODB4TEST.COMPANY.COM')
	cur = con.cursor()
	cur.execute("SELECT * FROM name)
	results = cur.fetchall()
//////////////////////////////////////////

From this last line I get the following error which I don't understand I'm very new to python and have no idea about this any help would be appreciated


//////////////////////////////////////////////

File "/home/dcroke/mdcFDACStringCall.py", line 21, in fetchbatchdata
    results = cur.fetchall()
NameError: global name 'cur' is not defined

//////////////////////////////////////////////


Thanks all, 
Shaun

[toc] | [next] | [standalone]


#28573

FromChris Angelico <rosuav@gmail.com>
Date2012-09-06 21:05 +1000
Message-ID<mailman.290.1346929532.27098.python-list@python.org>
In reply to#28571
On Thu, Sep 6, 2012 at 8:45 PM, shaun <shaun.wiseman91@gmail.com> wrote:
> ////CODE///////////////////////////////
>
> def databasebatchcall(self,tid, bid):
>         con=cx_Oracle.connect('user/user123@odb4.dcc.company/ODB4TEST.COMPANY.COM')
>         cur = con.cursor()
>         cur.execute("SELECT * FROM name)
>         results = cur.fetchall()
> //////////////////////////////////////////
>
> From this last line I get the following error which I don't understand I'm very new to python and have no idea about this any help would be appreciated
>
>
> //////////////////////////////////////////////
>
> File "/home/dcroke/mdcFDACStringCall.py", line 21, in fetchbatchdata
>     results = cur.fetchall()
> NameError: global name 'cur' is not defined
>
> //////////////////////////////////////////////
>

Not quite, actually. The traceback names a different function. Look in
your code for a 'fetchbatchdata' function; it looks like you need to
pass the cursor from one function to the other.

To further assist, we'd need to see more of the code; but for a guess,
I would say that you either need to make cur a function argument, or
else (since this is class) an instance member, which you'd reference
as 'self.cur'.

ChrisA

[toc] | [prev] | [next] | [standalone]


#28574

FromDave Angel <d@davea.name>
Date2012-09-06 07:05 -0400
Message-ID<mailman.291.1346929564.27098.python-list@python.org>
In reply to#28571
On 09/06/2012 06:45 AM, shaun wrote:
> Hi all,
>
>  I have a class which I create an object from in a different script but when its run I get an error at the last part of this method:
>
> ////CODE///////////////////////////////
>
> def databasebatchcall(self,tid, bid):
>         con=cx_Oracle.connect('user/user123@odb4.dcc.company/ODB4TEST.COMPANY.COM')
> 	cur = con.cursor()
> 	cur.execute("SELECT * FROM name)
> 	results = cur.fetchall()
> //////////////////////////////////////////
>
> >From this last line I get the following error which I don't understand I'm very new to python and have no idea about this any help would be appreciated
>
>
> //////////////////////////////////////////////
>
> File "/home/dcroke/mdcFDACStringCall.py", line 21, in fetchbatchdata
>     results = cur.fetchall()
> NameError: global name 'cur' is not defined
>
> //////////////////////////////////////////////
>
>
> Thanks all, 
> Shaun

is that really the function in line 21, or is it just a similar line? 
Notice the error gives you filename & line number.  I have no way to
check either of those, but you can and should.  What really stands out
is the function name the error occurs in:   fetchbatchdata()

You don't show us the source to that function.

-- 

DaveA

[toc] | [prev] | [next] | [standalone]


#28575

FromPeter Otten <__peter__@web.de>
Date2012-09-06 13:07 +0200
Message-ID<mailman.292.1346929651.27098.python-list@python.org>
In reply to#28571
shaun wrote:

>  I have a class which I create an object from in a different script but 
when its run I get an error at the last part of this method:

> def databasebatchcall(self,tid, bid):
>         con=cx_Oracle.connect(
>         'user/user123@odb4.dcc.company/ODB4TEST.COMPANY.COM')
>         cur = con.cursor()
>         cur.execute("SELECT * FROM name)
>         results = cur.fetchall()

This is not your real code. The above would give you a SyntaxError in the 
line

>         cur.execute("SELECT * FROM name)

> From this last line I get the following error which I don't understand I'm 
very new to python and have no idea about this any help would be appreciated

> File "/home/dcroke/mdcFDACStringCall.py", line 21, in fetchbatchdata
>     results = cur.fetchall()
> NameError: global name 'cur' is not defined

The offending line may not be indented correctly:

    def databasebatchcall(...):
        ...
        cur = con.cursor()
        ...
    results = cur.fetchall()

This may be obscured by mixing tabs and spaces. However, without the actual 
code this is nothing but a guess.

[toc] | [prev] | [next] | [standalone]


#28578

FromChris Angelico <rosuav@gmail.com>
Date2012-09-06 21:53 +1000
Message-ID<mailman.295.1346932440.27098.python-list@python.org>
In reply to#28571
On Thu, Sep 6, 2012 at 9:37 PM, shaun <shaun.wiseman91@gmail.com> wrote:
> class StringCall:
>         results=[]
>         def databasebatchcall(self,termid, batchid):
>                 con = cx_Oracle.connect('user/user123@odb4.dcc.company.ie/ODB4TEST.COMPANY.IE')
>                 cur = con.cursor()
>                 cur.execute("SELECT * from name)
>                 results = cur.fetchall()

This actually never sets 'results', which is a class variable. You
should probably be using 'self.results' here; Python does not include
class/instance members in scope automatically.

Try using 'self.' for everything that you need to be maintained as
state, and see if that solves your problem.

But this looks to me like a case where you may not really even need a
class at all.

ChrisA

[toc] | [prev] | [next] | [standalone]


#28580

Fromshaun <shaun.wiseman91@gmail.com>
Date2012-09-06 05:07 -0700
Message-ID<1c670c04-4367-4518-aa50-cf9adad8f6d2@googlegroups.com>
In reply to#28571
Hi Chris,

     I'm changing it into multiple classes because the script is going to get much larger its more for maintainability reasons rather than functionality reasons.

Thanks so much man it was the "self" fix you stated above. I woe you a pint of Guinness :D

Thanks again,
Shaun

[toc] | [prev] | [next] | [standalone]


#28587

FromChris Angelico <rosuav@gmail.com>
Date2012-09-06 23:16 +1000
Message-ID<mailman.300.1346937389.27098.python-list@python.org>
In reply to#28580
On Thu, Sep 6, 2012 at 10:07 PM, shaun <shaun.wiseman91@gmail.com> wrote:
> Hi Chris,
>
>      I'm changing it into multiple classes because the script is going to get much larger its more for maintainability reasons rather than functionality reasons.

Doesn't necessarily have to be multiple classes. Python gives you the
flexibility of dividing things in whatever way makes sense to your
project. Maybe a class is right - I can't say without seeing all your
code and knowing all your intentions - but if it's not, you don't have
to feel constrained by it. This isn't Java where all code goes into a
class!

> Thanks so much man it was the "self" fix you stated above. I woe you a pint of Guinness :D

You're welcome! I'm a non-drinker, though, so you can drink it and
think of me. :)

ChrisA

[toc] | [prev] | [next] | [standalone]


#28581

FromMRAB <python@mrabarnett.plus.com>
Date2012-09-06 13:08 +0100
Message-ID<mailman.296.1346933317.27098.python-list@python.org>
In reply to#28571
On 06/09/2012 12:37, shaun wrote:
> Sorry guys here is the full code for the class:
>
> #!/usr/bin/python
> # Echo client program
> import cx_Oracle
> import socket
> import pprint
> from struct import *
> import sys
> from binascii import *

Don't use "from something import *". It'll import a whole load of
names. Import only those names you wish to use.

> import time
> import datetime
>
>
> class StringCall:
> 	results=[]
> 	def databasebatchcall(self,termid, batchid):
> 		con = cx_Oracle.connect('user/user123@odb4.dcc.company.ie/ODB4TEST.COMPANY.IE')
> 		cur = con.cursor()
> 		cur.execute("SELECT * from name)

That line has an unterminated string literal (missing quote). That
means that this file won't compile.

> 		results = cur.fetchall()

As you're binding to "results" in the method, Python will assume that
that name is local to the method. The results will be discarded as soon
as the method returns, which it does right after.
> 		
> 	
> 	def fetchbatchdata(self,results):
> 		
> 		for row in results:
> 			mer = row[0].ljust(25, ' ')
> 			mercity = row[1].ljust(13, ' ')
> 			mertype = row[2]
> 			merloc = row[3]
> 			mercount = row[4]
> 			mersec = row[5]
> 			acq = row[6]
> 			btime = row[7].strftime('%d%m')
> 			bmerch = str(row[8]).rjust(12, '0')
> 			termcur = row[9]
> 			acqbank = str(row[10]).rjust(24, '0')
> 			termtype = row[11]
> 			termsoftver = row[12]
> 			merbatch = str(row[13]).rjust(3, '0')
> 			reccount = str(row[14]).rjust(9, '0')
> 			amounttotal = str(row[15]).rjust(16, '0')
> 			cashback = str(row[16]).rjust(16, '0')
> 			deposit = str(row[17]).rjust(16, '0')

All of the names "mer", "mercity", etc, will be local to this method.
> 		
> 	def createbatchstrings(self):
> 		BatchHeaderPacket = "\x01000\x0251.520%s00000%s000006060001%s%s%s%s0003 \x03" % (btime, bmerch, termcur, acqbank, termtype, termsoftver);
> 		ParameterPacket = "\x01001\x0251.5300000401%s%sIE%s%s%s00000%s%s0%s                    \x03" % (mer, mercity, mertype, merloc, termid, mercount, mersec, acq);
> 		TrailerPacket = "\x01003\x0251.550%s00%s%s%s%s%s00000000000\x03" % (btime, merbatch, reccount, amounttotal, cashback, deposit);
> 		cur.close()

Where do the names "btime", "bmerch", etc, come from? They are
certainly not the same as those in "fetchbatchdata" because this is a
separate method.

>
> 	def returnbatchheader(self):
> 		return BatchHeaderPacket
> 	def returnparameterpacket(self):
> 		return ParameterPacket
> 	def returntrailerpacket(self):
> 		return TrailerPacket
> 		

[toc] | [prev] | [next] | [standalone]


#28642

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-09-06 17:16 -0400
Message-ID<mailman.329.1346966229.27098.python-list@python.org>
In reply to#28571
On Thu, 6 Sep 2012 04:37:04 -0700 (PDT), shaun
<shaun.wiseman91@gmail.com> declaimed the following in
gmane.comp.python.general:


> 
> class StringCall:
> 	results=[]
> 	def databasebatchcall(self,termid, batchid):

	What are termid and batchid -- you pass them into the method, and
NEVER USE THEM!

> 		con = cx_Oracle.connect('user/user123@odb4.dcc.company.ie/ODB4TEST.COMPANY.IE')
> 		cur = con.cursor()
> 		cur.execute("SELECT * from name)

	con and cur are locals to this function/method

> 		results = cur.fetchall()
> 	
	results is also a local... All three disappear when the method
returns

	I suspect you need to study the language reference regarding classes
and function/methods, argument passing, and the use of "self"

> 	
> 	def fetchbatchdata(self,results):
> 		
> 		for row in results:
> 			mer = row[0].ljust(25, ' ')
> 			mercity = row[1].ljust(13, ' ')
> 			mertype = row[2]
> 			merloc = row[3]
> 			mercount = row[4]
> 			mersec = row[5]
> 			acq = row[6]
> 			btime = row[7].strftime('%d%m')
> 			bmerch = str(row[8]).rjust(12, '0')
> 			termcur = row[9]
> 			acqbank = str(row[10]).rjust(24, '0')
> 			termtype = row[11]
> 			termsoftver = row[12]
> 			merbatch = str(row[13]).rjust(3, '0')
> 			reccount = str(row[14]).rjust(9, '0')
> 			amounttotal = str(row[15]).rjust(16, '0')
> 			cashback = str(row[16]).rjust(16, '0')
> 			deposit = str(row[17]).rjust(16, '0')
> 		
	I'd reserve all the formatting for when you need to output the data;
that would permit using tuple unpacking

	(mer, mercity, mertype, ...
		cashback, deposit) = row

	But note that after you've filled all those names with the first
record -- you throw the first record away with the data from the second
record, and repeat for each record... When the loop ends, all those
names only reference the last record's data...

	AND then the method finishes and all those names are garbage
collected.

> 	def createbatchstrings(self):
> 		BatchHeaderPacket = "\x01000\x0251.520%s00000%s000006060001%s%s%s%s0003 \x03" % (btime, bmerch, termcur, acqbank, termtype, termsoftver);
> 		ParameterPacket = "\x01001\x0251.5300000401%s%sIE%s%s%s00000%s%s0%s                    \x03" % (mer, mercity, mertype, merloc, termid, mercount, mersec, acq);
> 		TrailerPacket = "\x01003\x0251.550%s00%s%s%s%s%s00000000000\x03" % (btime, merbatch, reccount, amounttotal, cashback, deposit);
> 		cur.close()
>
	All the xxxxPacket names are local and will be thrown away after the
method returns... 

	And cur, mer, mercity, etc. are UNDEFINED locals here...


> 	def returnbatchheader(self):
> 		return BatchHeaderPacket
> 	def returnparameterpacket(self):
> 		return ParameterPacket
> 	def returntrailerpacket(self):
> 		return TrailerPacket
>
	None of these "return xxxx" will work, since the "xxxx" are all
undefined local names.

	And these methods are superfluous, why not just have
"createBatchStrings" return all three as a tuple.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web