import custom module from controller

165 views
Skip to first unread message

mika

unread,
May 29, 2009, 5:38:19 AM5/29/09
to web2py Web Framework
As in topic, I have a problem with importing. I saw similar posts, but
they don't help me to solve it.
I have a file called addons.py in modules/ directory.
In controller, i'm trying to import like this:

import applications.sara.modules.addons as addons
(sara is the name of my app)

What I'm doing wrong?

Iceberg

unread,
May 29, 2009, 7:54:21 AM5/29/09
to web2py Web Framework
It should be ok. Guess you have to dive into your specific error
ticket.

Just in case, make sure there is an __init__.py inside applications/
yourapp and applications/yourapp/modules. (By default there already
has one. But who knows...)

mika

unread,
May 29, 2009, 10:02:38 AM5/29/09
to web2py Web Framework
Ok, now it seems to work.
But I don't exactly know where was the problem ;)

MikeEllis

unread,
Jun 26, 2009, 10:20:58 AM6/26/09
to web2py Web Framework
I just ran into what may be the same problem. I created a python
module in my apps modules directory and it showed up in the admin page
listings, could be edited, etc, but trying to import in a controller
kept failing. After I restarted web2py, the import succeeded. Is
there some documentation regarding what should be expected with regard
to importing new or changed modules, either from the modules directory
or from other locations in sys.path? I bought a copy of the manual and
read it, but it doesn't seem to address this issue. Having to restart
the server doesn't seem like the best of all possible worlds :-)

Thanks,
Mike

MikeEllis

unread,
Jun 26, 2009, 10:42:39 AM6/26/09
to web2py Web Framework
Arrgh! spoke too soon. Problem reappeared the second time I visited
the index page. Here's the ticket ...

Error traceback
Traceback (most recent call last):
File "/Users/mellis/sources/trunk/python/web2py/gluon/
restricted.py", line 107, in restricted
exec ccode in environment
File "/Users/mellis/sources/trunk/python/web2py/applications/pda/
controllers/default.py", line 10, in <module>
import rewriter
ImportError: No module named rewriter



In file: /Users/mellis/sources/trunk/python/web2py/applications/pda/
controllers/default.py
# coding: utf8

#########################################################################
## This is a samples controller
## - index is the default action of any application
## - user is required for authentication and authorization
## - download is for downloading files uploaded in the db (does
streaming)
## - call exposes all registered services (none by default)
#########################################################################
import rewriter

<snip>

and here's the console output from the time I restarted web2py:

WARNING:root:unable to import dbhash
default applications appear to be installed already
web2py Enterprise Web Framework
Created by Massimo Di Pierro, Copyright 2007-2009
Version 1.64.3 (2009-06-19 07:35:40)
Database drivers available: SQLite3
Starting cron...
please visit:
http://127.0.0.1:8000
use "kill -SIGTERM 14006" to shutdown the web2py server
DEBUG:pda:web2py application pda starting


and, finally, here's an ls of my application's directories:

michael-elliss-macbook:trunk mellis$ ls -R python/web2py/applications/
pda
ABOUT app.log databases modules tests
LICENSE cache errors private uploads
__init__.py controllers languages sessions views
__init__.pyc cron models static

python/web2py/applications/pda/cache:
cache.lock

python/web2py/applications/pda/controllers:
appadmin.py default.py default.py.bak

python/web2py/applications/pda/cron:

python/web2py/applications/pda/databases:
<snip>


python/web2py/applications/pda/errors:
<snip>

python/web2py/applications/pda/languages:
fr-fr.py it.py pl.py pt-pt.py
it-it.py pl-pl.py pt-br.py pt.py

python/web2py/applications/pda/models:
db.py log.py menu.py
db.py.bak log.py.bak menu.py.bak

python/web2py/applications/pda/modules:
__init__.py __init__.pyc rewriter.py rewriter.py.bak rewriter.pyc

python/web2py/applications/pda/private:

python/web2py/applications/pda/sessions:
<snip>

python/web2py/applications/pda/static:
base.css calendar.css calendar.js jquery.js title.png

python/web2py/applications/pda/tests:

python/web2py/applications/pda/uploads:

python/web2py/applications/pda/views:
appadmin.html generic.json layout.html.bak
askuser.html.bak generic.rss layout.original.html
default generic.xml web2py_ajax.html
generic.html layout.html

python/web2py/applications/pda/views/default:
askuser.html index.html
askuser.html.bak user.html


Any help appreciated.

Thanks,
Mike

DenesL

unread,
Jun 26, 2009, 10:52:28 AM6/26/09
to web2py Web Framework
You should be using:

exec('from applications.%s.modules import xx as
yy'%request.application)
reload(yy)

the reload is only required during development when the module code
might change.
This also provides application name independence.

MikeEllis

unread,
Jun 26, 2009, 11:16:32 AM6/26/09
to web2py Web Framework
Thanks! That works (but wouldn't it make more sense for web2py to
include the application's modules directory in sys.path?)
Mike
Reply all
Reply to author
Forward
0 new messages