type 'exceptions.NameError' global name is not defined

2079 views
Skip to first unread message

chienr

unread,
Sep 15, 2008, 1:35:57 AM9/15/08
to web.py
Hi,

I'm new to web.py and have been struggling with this problem for the
past day. I hope someone can point me to where the problem is. I have
a for loop in a template like this:

$for line in lines:
$csv = line.rstrip().split(",")

But it's generating this error:

<type 'exceptions.NameError'> at /home
global name 'csv' is not defined

What does it mean?

Thanks,
Robert

Anand Chitipothu

unread,
Sep 15, 2008, 1:54:51 AM9/15/08
to we...@googlegroups.com
> $for line in lines:
> $csv = line.rstrip().split(",")
>

It should be:

$for line in lines:
$ csv = line.rstrip().split(",")

(notice the space before csv.)

chienr

unread,
Sep 15, 2008, 12:42:20 PM9/15/08
to web.py
That worked! Thanks.

chienr

unread,
Sep 15, 2008, 10:44:48 PM9/15/08
to web.py
Now I have a new but similar problem.

$for line in linex:
$ csv = line.rstrip().split(",")
$ ltime = time.localtime(csv[2])

yields this error:

global name 'time' is not defined

ashok raavi

unread,
Sep 16, 2008, 12:36:22 AM9/16/08
to we...@googlegroups.com

I think you are referring to the python's built-in time module, if so you have to define the time in template globals first.

#in your code
import time

web.template.Template.globals['time'] = time
--
ashok raavi

chienr

unread,
Sep 16, 2008, 12:47:51 AM9/16/08
to web.py
You're right, I am referring to the time module. And defining it in
globals works, thanks, but is this typical practice in web.py
applications? I saw some note about the upcoming 0.3 release moving
away from globals, so am I using templates in an unconventional way?
What's the correct way to do this?

Anand Chitipothu

unread,
Sep 16, 2008, 12:50:38 AM9/16/08
to we...@googlegroups.com
On Tue, Sep 16, 2008 at 10:17 AM, chienr <chi...@gmail.com> wrote:
>
> You're right, I am referring to the time module. And defining it in
> globals works, thanks, but is this typical practice in web.py
> applications? I saw some note about the upcoming 0.3 release moving
> away from globals, so am I using templates in an unconventional way?
> What's the correct way to do this?

In 0.3, the correct way to use globals is this:

globals = {'time': time}
render = web.template.render('templates', globals=globals)

See http://webpy.org/docs/0.3/templetor for more details.

rahul kumar

unread,
Sep 18, 2018, 4:04:52 AM9/18/18
to web.py
You do not have variable defined within the function or at the global level.

Anand Chitipothu

unread,
Sep 18, 2018, 7:21:19 AM9/18/18
to webpy
The web.py templates are a a bit sensitive to space.

You need to add a space after the $

     $ csv = line.rstrip().split(",") 

Anand
Reply all
Reply to author
Forward
0 new messages