UnboundLocalError: local variable referenced before assignment

Peter Otten __peter__ at web.de
Tue Jun 8 06:41:33 EDT 2010


Chris Rebert wrote:

> On Tue, Jun 8, 2010 at 2:00 AM, ch1zra <ch1zra at gmail.com> wrote:
>> On Jun 8, 10:29 am, Richard Thomas <chards... at gmail.com> wrote:
>>> On Jun 8, 9:03 am, ch1zra <ch1... at gmail.com> wrote:
>>> > I have following code :
>>>
>>> > import os, time, re, pyodbc, Image, sys
>>> > from datetime import datetime, date, time
>>> > from reportlab.lib.pagesizes import A4
>>> > from reportlab.lib.units import cm
>>> > from reportlab.pdfgen import canvas
>>> > from reportlab.pdfbase import pdfmetrics
>>> > from reportlab.pdfbase.ttfonts import TTFont
>>> > import mkTable
>>>
>>> > mkTable.mkTable()
>>>
>>> > and then file mkTable.py located in same directory has :
>>>
>>> > def mkTable():
>>> > global canvas
>>> > canvas = canvas.Canvas(fname, pagesize=A4)
>>> > ... and so on
>>>
>>> > this gives me following traceback:
>>>
>>> > Traceback (most recent call last):
>>> > File "C:\py\pdf_test.py", line 36, in <module>
>>> > mkTable.mkTable()
>>> > File "C:\py\mkTable.py", line 38, in mkTable
>>> > canvas = canvas.Canvas("K_lista.pdf", pagesize=A4)
>>> > UnboundLocalError: local variable 'canvas' referenced before
>>> > assignment
>>>
>>> > i haven't posted entire code, because those lines are giving me
>>> > problems. I've searched the web, and all say that I should make var
>>> > global inside function. so I did it, but still nothing...
>>> > using python 2.6.2 and reportlab 2.4
>>> > help plz :)
>>>
>>> The version of mkTable.py you posted is clearly different to the one
>>> in the traceback. You might want to check that.
>>>
>>> Richard.
>>
>> here's full version of mkTable.py (I've cut out all the drawing code
>> for clarity)
>> http://bpaste.net/show/7003/
>>
>> and it's caller :
>> http://bpaste.net/show/7004/
>>
>> and the newest traceback (just generated):
>>
>> Traceback (most recent call last):
>> File "C:\py\pdf_test.py", line 36, in <module>
>> mkTable.mkTable()
>> File "C:\py\mkTable.py", line 38, in mkTable
>> canvas.setFillColorRGB(0.8,0.8,0.8)
>> UnboundLocalError: local variable 'canvas' referenced before assignment
> 
> mkTable.py:
> # -*- coding: utf-8 -*-
> def mkTable():
>     global canvas
> <snip>
>     canvas = canvas.Canvas("K_lista.pdf", pagesize=A4)
> 
> The only global variable defined in mkTable.py is the "mkTable"
> function (partly since you don't import anything). So the reference to
> the global variable "canvas" on the right-hand side of this expression
> is completely undefined, resulting in the error you're getting.

The above would produce a NameError, not an UnboundLocalError:

>>> def foo():
...     global x
...     x = x
...
>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in foo
NameError: global name 'x' is not defined

To get the latter you'd have to omit the global declaration.

Peter



More information about the Python-list mailing list