confusion about web2py's MVC namespace

22 views
Skip to first unread message

小谢

unread,
Jul 1, 2009, 12:08:05 AM7/1/09
to web2py Web Framework
Recently I'm having some trouble with web2py but I don't know where
the problem is. My best guess is that it's about the MVC namespace.
Please correct me if I'm wrong.

The problem is that I defined a class "App" in models/utils.py and
when I tried to inherit this class in another .py file under the
models directory, web2py told me that class "App" is not found. I was
confused and moved class "App" to db.py(where database connection is
defined) and *bang*, it worked. No more error ticket and class "App"
is successfully inherited.

I'm not that familiar with web2py internals. Why did this happen? Is
there any resolution order or something?

Thanks


-- Xie

Iceberg

unread,
Jul 1, 2009, 12:41:32 AM7/1/09
to web2py Web Framework
web2py loads models in alphabetic order. So you need to make sure your
basic definition comes prior to your "another .py file". For example,
"models/0_setting.py" is kind of de facto convention.

However, I would recommend you to put your basic things inside modules/
whatever.py, this way you might have more flexibility.

Yarko Tymciurak

unread,
Jul 1, 2009, 1:09:55 AM7/1/09
to web...@googlegroups.com
Hi Xie -

The reason this worked as you described:

For each request to your application,  web2py runs, finds your (the request) application,
executes each file in the models directory (since the connection needs to be defined),
and then calls the controller file and function in controllers.

When you put a file in modules, you need to be sure it is imported, and the definitions available to your controllers.

To do this you need to:

  • put an empty (at least)  __init__.py    file in your application's   modules  folder (this marks it as a module)
  • somewhere that you know get's executed, e.g. in your models folder,  put an import statement.
For example, in applications/my_app/models/0.py,  add a line like this:

from applications.my_app.modules.utils import App

Hope this helps.

Regards,
Yarko

小谢

unread,
Jul 1, 2009, 10:19:58 AM7/1/09
to web2py Web Framework


On Jul 1, 1:09 pm, Yarko Tymciurak <yark...@gmail.com> wrote:
> Hi Xie -
>
> The reason this worked as you described:
>
> For each request to your application,  web2py runs, finds your (the request)
> application,
> executes each file in the models directory (since the connection needs to be
> defined),
> and then calls the controller file and function in controllers.
>
> When you put a file in modules, you need to be sure it is imported, and the
> definitions available to your controllers.
>
> To do this you need to:
>
>    - put an empty (at least)  __init__.py    file in your application's
>    modules  folder (this marks it as a module)
>    - somewhere that you know get's executed, e.g. in your models folder,
>    put an import statement.
>
> For example, in applications/my_app/models/0.py,  add a line like this:
>
> from applications.my_app.modules.utils import App
>
> Hope this helps.
>
> Regards,
> Yarko
>
> On Tue, Jun 30, 2009 at 11:41 PM, Iceberg <iceb...@21cn.com> wrote:
>
> > On Jul1, 12:08pm, Xie <mft...@gmail.com> wrote:
> > > Recently I'm having some trouble with web2py but I don't know where
> > > the problem is. My best guess is that it's about the MVC namespace.
> > > Please correct me if I'm wrong.
>
> > > The problem is that I defined a class "App" in models/utils.py and
> > > when I tried to inherit this class in another .py file under the
> > > models directory, web2py told me that class "App" is not found. I was
> > > confused and moved class "App" to db.py(where database connection is
> > > defined) and *bang*, it worked. No more error ticket and class "App"
> > > is successfully inherited.
>
> > > I'm not that familiar with web2py internals. Why did this happen? Is
> > > there any resolution order or something?
>
> > > Thanks
>
> > > -- Xie
>
> > web2py loads models in alphabetic order. So you need to make sure your
> > basic definition comes prior to your "another .py file".  For example,
> > "models/0_setting.py" is kind of de facto convention.
>
> > However, I would recommend you to put your basic things inside modules/
> > whatever.py, this way you might have more flexibility.
>
>

Thanks. It really helped. It's lucky that the project is still in its
early stage and we'll be refactoring the existing code with respect to
web2py's conventions.

Yarko Tymciurak

unread,
Jul 1, 2009, 2:30:47 PM7/1/09
to web...@googlegroups.com
On Wed, Jul 1, 2009 at 9:19 AM, 小谢 <mft...@gmail.com> wrote:
......
Thanks. It really helped. It's lucky that the project is still in its
early stage and we'll be refactoring the existing code with respect to
web2py's conventions.


Glad it helped.  

Of course, you do not need to import for every request (overhead) if you use your utility only sometimes....

then, you can just import the module / utility locally, where (and when) it's needed.

小谢

unread,
Jul 2, 2009, 3:19:19 AM7/2/09
to web2py Web Framework
In .py files in models directory, "__all__" doesn't work, does it?

Yarko Tymciurak

unread,
Jul 2, 2009, 4:55:42 AM7/2/09
to web...@googlegroups.com
On Thu, Jul 2, 2009 at 2:19 AM, 小谢 <mft...@gmail.com> wrote:

On Jul 2, 2:30 am, Yarko Tymciurak <yark...@gmail.com> wrote:
> On Wed, Jul 1, 2009 at 9:19 AM, Сл <mft...@gmail.com> wrote:
>
> ......
>
> > Thanks. It really helped. It's lucky that the project is still in its
> > early stage and we'll be refactoring the existing code with respect to
> > web2py's conventions.
>
> Glad it helped.
>
> Of course, you do not need to import for every request (overhead) if you use
> your utility only sometimes....
>
> then, you can just import the module / utility locally, where (and when)
> it's needed.

In .py files in models directory, "__all__" doesn't work, does it?

I think you mean something like:


    from applications/my_app/util  import *




小谢

unread,
Jul 2, 2009, 5:04:38 AM7/2/09
to web2py Web Framework
No, I mean, for example, there are three classes A, B, C in models/
0.py. Then these three classes are visible in the global MVC
namespace. You have no means to prevent the other model/control class/
func from using them and there is risk of potential name collision
(custom class A vs web2py's html class A), right?

Yarko Tymciurak

unread,
Jul 2, 2009, 5:11:34 AM7/2/09
to web...@googlegroups.com
2009/7/2 小谢 <mft...@gmail.com>

I think you would have to check this yourself, programmatically.
 



小谢

unread,
Jul 2, 2009, 6:18:39 AM7/2/09
to web2py Web Framework
Sure, and it's easy with "globals()". This forces the programmers to
encapsulate a bunch of related classes in modules. I just think that
life would be easier if "__all__" still works in model/*.py, as my
previous experiences mainly came from Java.

mdipierro

unread,
Jul 2, 2009, 9:06:43 AM7/2/09
to web2py Web Framework
exec('from applications.%s.modules.util import *' %
request.application)

assuming util.py is under modules
Reply all
Reply to author
Forward
0 new messages