UnboundLocalError: local variable referenced before assignment

Discussion in 'Python' started by ch1zra, Jun 8, 2010.

  1. ch1zra

    ch1zra Guest

    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 :)
    ch1zra, Jun 8, 2010
    #1
    1. Advertising

  2. On Jun 8, 9:03 am, ch1zra <> 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.
    Richard Thomas, Jun 8, 2010
    #2
    1. Advertising

  3. ch1zra

    Bryan Guest

    ch1zra 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


    Python doesn't have one global namespace. Each module (file) has its
    own namespace, which is a Python dict, and 'global' means defined in
    the containing module's dict. Put the import:

    from reportlab.pdfgen import canvas

    in the mkTable.py file. That brings 'canvas' into the mkTable module's
    namespace.

    Python programs commonly import the same module multiple times. Only
    the first import runs the body of the imported module. Subsequent
    imports merely bring the names into the importing module's namespace.


    --
    --Bryan Olson
    Bryan, Jun 8, 2010
    #3
  4. ch1zra

    ch1zra Guest

    On Jun 8, 10:29 am, Richard Thomas <> wrote:
    > On Jun 8, 9:03 am, ch1zra <> 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
    ch1zra, Jun 8, 2010
    #4
  5. ch1zra

    Chris Rebert Guest

    On Tue, Jun 8, 2010 at 2:00 AM, ch1zra <> wrote:
    > On Jun 8, 10:29 am, Richard Thomas <> wrote:
    >> On Jun 8, 9:03 am, ch1zra <> 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.

    In this respect, your code is akin to:
    whatever.py:
    # -*- coding: utf-8 -*-
    def foo():
    global x
    x = 2 * x
    foo()
    # This is obviously horribly flawed since x is never given an initial value!
    #EOF

    More generally, your code uses "canvas" to refer both to the module
    reportlab.pdfgen.canvas and an instance of the class
    reportlab.pdfgen.canvas.Canvas; this is confusing and problematic. I
    suggest you either rename one of them to something distinct (e.g.
    `import reportlab.pdfgen.canvas as pdfcanvas`), or import just the
    class Canvas straight from reportlab.pdfgen.canvas (i.e. `from
    reportlab.pdfgen.canvas import Canvas`).

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris Rebert, Jun 8, 2010
    #5
  6. ch1zra

    ch1zra Guest

    On Jun 8, 10:59 am, Bryan <> wrote:
    > ch1zra 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

    >
    > Python doesn't have one global namespace. Each module (file) has its
    > own namespace, which is a Python dict, and 'global' means defined in
    > the containing module's dict. Put the import:
    >
    >   from reportlab.pdfgen import canvas
    >
    > in the mkTable.py file. That brings 'canvas' into the mkTable module's
    > namespace.
    >
    > Python programs commonly import the same module multiple times. Only
    > the first import runs the body of the imported module. Subsequent
    > imports merely bring the names into the importing module's namespace.
    >
    > --
    > --Bryan Olson


    thanx so much, it worked.
    I am getting some other problems now, but will use this logic to fix
    it.
    thanx once again :)
    ch1zra, Jun 8, 2010
    #6
  7. ch1zra

    Peter Otten Guest

    Chris Rebert wrote:

    > On Tue, Jun 8, 2010 at 2:00 AM, ch1zra <> wrote:
    >> On Jun 8, 10:29 am, Richard Thomas <> wrote:
    >>> On Jun 8, 9:03 am, ch1zra <> 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
    Peter Otten, Jun 8, 2010
    #7
  8. ch1zra

    Bryan Guest

    Peter Otten wrote:
    > Chris Rebert wrote:

    [...]
    > > 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:


    Ah, yes. The OP's code produces a NameError.

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


    Not hard to diagnose: The OP got the UnboundLocalError, so he looked
    stuff up and tried various things, such as the global declaration, but
    still got an exception. In writing it up, he copied the initial
    exception, but a latter version of the function.


    --
    --Bryan
    Bryan, Jun 8, 2010
    #8
  9. ch1zra a écrit :
    > On Jun 8, 10:59 am, Bryan <> wrote:
    >> Python doesn't have one global namespace. Each module (file) has its
    >> own namespace, which is a Python dict, and 'global' means defined in
    >> the containing module's dict. Put the import:
    >>
    >> from reportlab.pdfgen import canvas
    >>
    >> in the mkTable.py file. That brings 'canvas' into the mkTable module's
    >> namespace.
    >>

    >
    > thanx so much, it worked.
    > I am getting some other problems now,


    Yeps. One of them being rebinding mkTable.canvas on the first call to
    mkTable.mkTable...

    Get rid of the 'global canvas' at the beginning of the mkTable.mkTable
    function, and rename your local var to something else than canvas.
    Bruno Desthuilliers, Jun 8, 2010
    #9
  10. ch1zra

    danieldelay Guest

    Le 08/06/2010 10:03, ch1zra a écrit :
    > 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


    Hello,

    It does not really make sense to redefine an object you've imported,
    like in
    > import mkTable
    > def mkTable():


    or like in :
    > from reportlab.pdfgen import canvas
    > canvas = ...


    You must use differents names for the variables you wan't to use, or the
    function you wan't to import

    > from reportlab.pdfgen import canvas
    > ....
    > import mkTable
    > ...
    > def MYmkTable():
    > ...
    > MYcanvas = canvas.Canvas(fname, pagesize=A4)


    Daniel
    danieldelay, Jun 8, 2010
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Moscatt
    Replies:
    2
    Views:
    9,792
    Peter Moscatt
    Mar 19, 2005
  2. Replies:
    2
    Views:
    5,275
    Diez B. Roggisch
    Feb 26, 2006
  3. Wang, Harry
    Replies:
    2
    Views:
    302
  4. Gabriel Rossetti
    Replies:
    3
    Views:
    944
    Hrvoje Niksic
    Dec 9, 2009
  5. Replies:
    9
    Views:
    87
    John Gordon
    Jun 24, 2013
Loading...

Share This Page