Problem with import in controllers

20 views
Skip to first unread message

voltron

unread,
Apr 30, 2008, 9:28:56 AM4/30/08
to web2py Web Framework
I have a module in the controllers folder of an application called
"amf_accounts". I put an import statement in a controller called
"amf" , even Eclipse does the autocompletion and does not complain.
But when I try to call a function in the "amf" controller, it
complains that it cannot import "amf_accounts", even putting an
__init__ file in the controllers folder did not help. Is there
something Im doing wromg?

Thanks

Massimo Di Pierro

unread,
Apr 30, 2008, 9:46:00 AM4/30/08
to web...@googlegroups.com
can you send me your import statement? Are you importing a module
(ok) or a controller (not ok)?

Massimo

voltron

unread,
Apr 30, 2008, 1:50:27 PM4/30/08
to web2py Web Framework
HI Massimo, its nothing complicated, I even tried with something very
simple like this:

# This is a test module to import test.py
def testFunc():
print "test"


both controller and modules are py files, there is no distinction that
I know of. I just wanted to relocate all the functions that belong to
a group in separate files, and then import the separte mfunctions in
the modukes in the controller called amf

Massimo Di Pierro

unread,
Apr 30, 2008, 2:11:15 PM4/30/08
to web...@googlegroups.com
Sorry voltron. I cannot tell what the problem is from the information
I have. Feel free to send me the files and I will look at them.

Massimo

Massimo Di Pierro

unread,
Apr 30, 2008, 2:45:30 PM4/30/08
to web...@googlegroups.com
I see the problem. You are using

import amf_accounts
import amf_scoring

but the amf.py controller is not imported, it is executed. Therefore
it has no knowledge of its location and the two import above are not
resolved in the controllers folder.
You need to do the usual:

import applications.yourapp.controllers.amf_accounts as amf_accounts

I would put them in modules not in controllers anyway since that
would not buy you anything.

Massimo


On Apr 30, 2008, at 12:50 PM, voltron wrote:

voltron

unread,
Apr 30, 2008, 2:52:46 PM4/30/08
to web2py Web Framework
aha, okay thanks!

voltron

unread,
May 2, 2008, 5:21:35 AM5/2/08
to web2py Web Framework
Hi Massimo,

I am still having problems. I can import the modules for the modules
folder, but now web2py complains of a glabal name "db" not being
defined, this is strange as I thought that the modules in the modules
folder is not executed till it is imported in the calling controller.
Do you have a solution for me?


Thanks

Massimo Di Pierro

unread,
May 2, 2008, 10:54:36 AM5/2/08
to web...@googlegroups.com
db is defined in your models and therefore it is not visible to
modules. If you want to use functions/classes from modules you have
to explicitly pass db,request,response,cache,session to them.

voltron

unread,
May 2, 2008, 10:59:54 AM5/2/08
to web2py Web Framework
then this is the advantage of leaving them in the controllers folders?

Massimo Di Pierro

unread,
May 2, 2008, 11:03:10 AM5/2/08
to web...@googlegroups.com
or is a model file. yes. That is why the are executed and not imported.

voltron

unread,
May 2, 2008, 11:28:58 AM5/2/08
to web2py Web Framework
now I am confused

exec('import applications.%s.controllers.amf_accounts as amf_accounts'
% request.application)

does not work either

Massimo Di Pierro

unread,
May 2, 2008, 11:45:28 AM5/2/08
to web...@googlegroups.com
the difference between import .... and exec('import ...') is only
that you can specify the appname in the path. You are still importing
the module and when you import it the module does not see the global
variables defined outside/before the module. If that is what you are
trying to do you should

execfile(path_to_module_with_py)

Reply all
Reply to author
Forward
0 new messages