importing modules questions

27 views
Skip to first unread message

DenesL

unread,
Mar 11, 2008, 8:04:48 PM3/11/08
to web2py Web Framework
Using trunk 84.

I created cookbook/modules/module1.py (and cookbook/module/
__init__.py).
Now from a function in cookbook/controllers/test I import specific
function names (or import *) from module1.
Inside the module1 function I test if isinstance(x,IS_IN_SET)
but that fails with
NameError: global name 'IS_IN_SET' is not defined
(I have no problem using IS_IN_SET inside controller/test).

Also:
the first time I ran the app I had a typo in module1 so I received a
ticket with:
NameError: global name 'typo' is not defined.
Went back, fixed the error and ran it again. Another ticket showing me
the fixed line but still giving me the same error:
NameError: global name 'typo' is not defined.
I noticed that module1.pyc was older then module1.py so I deleted it.
Ran again, same error, no new module1.pyc created.
Deleted __init__.pyc too, same error, no pycs created.
Did cleanup on cookbook, same error!.
Stop server, start server, same error!!.
Shutdown web2py, restart it, error gone.. pffiu.

Am I doing something wrong?.

limodou

unread,
Mar 11, 2008, 8:41:54 PM3/11/08
to web...@googlegroups.com

Can you post your code?

--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
meide <<wxPython UI module>>: http://code.google.com/p/meide/
My Blog: http://www.donews.net/limodou

Michael Wills

unread,
Mar 11, 2008, 9:09:27 PM3/11/08
to web...@googlegroups.com
IS_IN_SET and other validators are passed into controllers and models automatically but I perhaps not into custom modules. Until Massimo replies perhaps you can try to import IS_IN_SET into your module1 with:

from gluon.validators import IS_IN_SET

I work within Eclipse and have a command to run web2py from source so stop/restart is pretty quick. There are times when making changes that I do have to stop and restart web2py to clear up an error. Unfortunately I still haven't seen something completely consistent that I discern why it happens.

DenesL

unread,
Mar 11, 2008, 10:06:30 PM3/11/08
to web2py Web Framework
Not at the moment but module1 has a class and a function.
The function accepts a form (SQLFORM to be more precise) and among
other things checks if the form's fields have IS_IN_SET validators to
extract all the options. I will be using the module to get all the
field values from the form so i can plug them into my own nicely
formatted form. The code currently works as part of the controller.


On Mar 11, 7:41 pm, limodou <limo...@gmail.com> wrote:

Michael Wills

unread,
Mar 11, 2008, 11:12:41 PM3/11/08
to web...@googlegroups.com
I'll have to test and see if modules in the module folder receive all the automatic imports that controllers and models do. It may work in the controller because web2py adds IS_IN_SET, along with the other standard validators, HTML helpers, and other items into the globals when your controller is run. Those are imported at a higher level in the stack it seems. More on that in a bit... :-)

Michael Wills

unread,
Mar 11, 2008, 11:15:02 PM3/11/08
to web...@googlegroups.com
Oh yes, you did clarify one bit.

Defining a class and modifying that class often requires stopping and restarting web2py. Code that is not in a class is handled fine without the restart.

DenesL

unread,
Mar 11, 2008, 11:29:57 PM3/11/08
to web2py Web Framework
The class has nothing to do with the error.
It is an extended list with an xml() function to gather all of the
individual HTML helper elements xml() output.

On Mar 11, 10:15 pm, "Michael Wills" <mcwi...@gmail.com> wrote:
> Oh yes, you did clarify one bit.
>
> Defining a class and modifying that class often requires stopping and
> restarting web2py. Code that is not in a class is handled fine without the
> restart.
>
> On Tue, Mar 11, 2008 at 8:12 PM, Michael Wills <mcwi...@gmail.com> wrote:
> > I'll have to test and see if modules in the module folder receive all the
> > automatic imports that controllers and models do. It may work in the
> > controller because web2py adds IS_IN_SET, along with the other standard
> > validators, HTML helpers, and other items into the globals when your
> > controller is run. Those are imported at a higher level in the stack it
> > seems. More on that in a bit... :-)
>

Michael Wills

unread,
Mar 12, 2008, 4:05:46 AM3/12/08
to web...@googlegroups.com
Ah I didn't explain myself clearly enough. My apologies. I was referring to the 'typo' error where you fixed the typo but still received the NameError. I have found that when dealing with classes, i.e. defining and using your own custom classes, web2py needs to be restarted. Otherwise it will not fully refresh as you have found.

As for IS_IN_SET, you were able to use IS_IN_SET inside your custom module1 without specifically importing in into your module1? It would be good to know if that info is passed into custom modules.

limodou

unread,
Mar 12, 2008, 4:20:49 AM3/12/08
to web...@googlegroups.com
On Wed, Mar 12, 2008 at 4:05 PM, Michael Wills <mcw...@gmail.com> wrote:
> Ah I didn't explain myself clearly enough. My apologies. I was referring to
> the 'typo' error where you fixed the typo but still received the NameError.
> I have found that when dealing with classes, i.e. defining and using your
> own custom classes, web2py needs to be restarted. Otherwise it will not
> fully refresh as you have found.
>
> As for IS_IN_SET, you were able to use IS_IN_SET inside your custom module1
> without specifically importing in into your module1? It would be good to
> know if that info is passed into custom modules.
>
I also found that if I imported my personal modules in modules folder,
and then changed it, then refresh the web page, but the change didn't
affect at all. So I think it's probably python saved them. So I add
reload(module) for testing only, for example:

import applications.myapp.modules.mymodule as mod
reload(mod)

So, it'll be imported each time when I visit the page.

Michael Wills

unread,
Mar 12, 2008, 4:49:29 AM3/12/08
to web...@googlegroups.com
Ah yes I think Massimo mentioned that tip previously. One day web2py will be updated live via the web with that. haha :-P

DenesL

unread,
Mar 12, 2008, 11:15:06 AM3/12/08
to web2py Web Framework
Thanks Michael and limodou.

Reload fixes the issue with the typos.

The IS_IN_SET error just shows that I have to pay more attention.
From the Python manual:
"Each module has its own private symbol table, which is used as the
global symbol table by all functions defined in the module. Thus, the
author of a module can use global variables in the module without
worrying about accidental clashes with a user's global variables."

DenesL

unread,
Mar 12, 2008, 11:23:35 AM3/12/08
to web2py Web Framework
For future reference, in the imported module (module1 in this case)
you have to import all the required objects, as in:

from gluon.validators import IS_IN_SET

Michael Wills

unread,
Mar 12, 2008, 11:29:30 AM3/12/08
to web...@googlegroups.com
If you're using the built in web interface you should be able to do

from gluon.validators import *

to get them all in one go. Save some typing unless you're only using a couple specific ones.
Reply all
Reply to author
Forward
0 new messages