Importing modules from controller folder

11 views
Skip to first unread message

voltron

unread,
May 14, 2009, 12:47:25 PM5/14/09
to web2py Web Framework
I have several forms and accompanying functions, I would like to be
able to import them from the controller folder as in:

/controlllers
/forms/__init__
/forms/ reg_form.py

mycontroller.py


#in mycontroller.py


import forms.reg_form ### ERROR


This is not possible for some reason. I could have put them in the
"modules" folder, but that invilves import gluon.html.gluon.languages
e.t.c which is also a headache. Is there any workaround?


thanks

mdipierro

unread,
May 14, 2009, 2:49:16 PM5/14/09
to web2py Web Framework
put them in

applications/yourapp/modules

and

exec('import application.%s.modules.forms.reg_form as reg_form' %
request.application)

Voltron

unread,
May 14, 2009, 2:54:58 PM5/14/09
to web2py Web Framework
Done that, was a pain Massimo as I explained in my first post:

"This is not possible for some reason. I could have put them in the
"modules" folder, but that involves import gluon.html.gluon.languages
e.t.c which is also a headache. Is there any workaround?
"

it is a headache, I donot want to track every module or function that
I need to create, translate or validate forms in my forms module

Álvaro Justen [Turicas]

unread,
May 14, 2009, 3:10:13 PM5/14/09
to web...@googlegroups.com
On Thu, May 14, 2009 at 3:54 PM, Voltron <nhy...@googlemail.com> wrote:
>
> Done that, was a pain Massimo as I explained in my first post:
>
> "This is not possible for some reason. I could have put them in the
> "modules" folder, but that involves import gluon.html.gluon.languages
> e.t.c which is also a headache. Is there any workaround?
> "
>
> it is a headache, I donot want to track every module or function that
> I need to create, translate or validate forms in my forms module

A "package" is a path with __init__.py.
So if you want to include "applcations.yourapp.controllers.something",
all of these paths (applications, applications/yourapp,
applications/yourapp/welcome and
applications/yourapp/welcome/controllers) must have __init__.py empty
(generally...) file inside.

So, create __init__.py in applications/yourapp/controllers AND in
applications/yourapp/controllers/forms (you have __init__, not
__init__.py). So you can do:

import applications.yourapp.controllers.forms.reg_form as reg_form

--
Álvaro Justen
Peta5 - Telecomunicações e Software Livre
21 3021-6001 / 9898-0141
http://www.peta5.com.br/

Voltron

unread,
May 14, 2009, 3:58:38 PM5/14/09
to web2py Web Framework
I tried that out Alvar, I know about python packages, and I have been
using them for a while in the modules folder, but since its a
subfolder in the Controllers folder, the FORM class and other things
are missing.

Oh well

On May 14, 9:10 pm, Álvaro Justen [Turicas] <alvarojus...@gmail.com>
wrote:

Mark Larsen

unread,
May 14, 2009, 8:38:48 PM5/14/09
to web...@googlegroups.com
> /controlllers
>              /forms/__init__
>              /forms/ reg_form.py

You are missing .py on the __init__, was that a typo?

> import forms.reg_form ### ERROR

Since web2py runs out of it's root maybe (this as worked for me with
things out of my modules directory):

import applications.appName.controllers.forms.reg_form

mdipierro

unread,
May 15, 2009, 2:11:57 AM5/15/09
to web2py Web Framework
Hi voltron. was this resolved?

Glad to have you back from php. ;-)

Voltron

unread,
May 15, 2009, 3:23:17 AM5/15/09
to web2py Web Framework
Hi Massimo!

Yes, the problem still persists, it is not possible to import a
package or module from a subdirectory in the controller folder, even
when I placed an "__init__.py file in almost every folder

I still use Web2py for one domain, its just that my boss was not too
happy with the problems we had deploying the web2py applications
(CherryPY server not scaling, FCGI). We are using a PHP framework for
those domains( 3) instead

mdipierro

unread,
May 15, 2009, 8:57:23 AM5/15/09
to web2py Web Framework
Voltron, I do that all the time so something else is wrong. Can you
email be the relevant parts of the app (the imported controller and
the one being imported)?

I do not believe your scaling problem was with the web2py wsgiserver.
If I remember you had more than 100 requests per second on a single
machine. I think the problem was database bottleneck. Are the PHP and
web2py codes equivalent? Can you share any benchmark?

Massimo

Yarko Tymciurak

unread,
May 15, 2009, 9:01:24 AM5/15/09
to web...@googlegroups.com
On Fri, May 15, 2009 at 2:23 AM, Voltron <nhy...@googlemail.com> wrote:
.....


I still use Web2py for one domain, its just that my boss was not too
happy with the problems we had deploying the web2py applications
(CherryPY server not scaling, FCGI). We are using a PHP framework for
those domains( 3) instead


The builtin CherryPy server is convenient for development, and yet something like apache / mod_wsgi is the deployment of preference.  We'll have to write in more detail about deployment strategies in the community manual.

Regards,
- Yarko

DenesL

unread,
May 15, 2009, 11:08:43 AM5/15/09
to web2py Web Framework
On May 15, 8:57 am, mdipierro <mdipie...@cs.depaul.edu> wrote:
> Voltron, I do that all the time so something else is wrong.

Massimo, what is what you do all the time?
import from the controllers folder or import from modules?.

It seems to me that in web2py's design the controllers folders was not
meant for this, it has never included the requerired __init__.py files
to make it a package.

And even after you add the __init__.py files you still have to import
from gluon anything that you will use to create your form.

So the question becomes: is there a location to place forms (or other
code) so they can be imported without having to import all the other
parts from gluon?. If not, should there be one?.

mdipierro

unread,
May 15, 2009, 11:25:10 AM5/15/09
to web2py Web Framework
I import modules from the app all the time. I normally put them in
modules/ but it makes no difference if they are in controllers/. I
would not do it this way not nothing technically prevents it if you
have the __init__.py file.

You can define forms in modules and they will be visible everywehere.

You can define them in views if you only need them in views.

Voltron is very concerned about doing this as fast as possible, hence
they should be in modules so that the pyc are cached by Python.

Massimo

Álvaro Justen [Turicas]

unread,
May 15, 2009, 11:41:39 AM5/15/09
to web...@googlegroups.com
On Fri, May 15, 2009 at 4:23 AM, Voltron <nhy...@googlemail.com> wrote:
> Hi Massimo!
>
> Yes, the problem still persists, it is not possible to import a
> package or module from a subdirectory in the controller folder, even
> when I placed an "__init__.py file in almost every folder

I don't understand why you can't import them.
I just modified welcome app (from trunk) and it works very well for
me, like I stated before:

----- cut here -----


So, create __init__.py in applications/yourapp/controllers AND in
applications/yourapp/controllers/forms (you have __init__, not
__init__.py). So you can do:

import applications.yourapp.controllers.forms.reg_form as reg_form
----- cut here -----

Reply all
Reply to author
Forward
0 new messages