How to create a module

489 views
Skip to first unread message

Vikram

unread,
Dec 9, 2010, 12:44:12 AM12/9/10
to web2py-users, susha...@gmail.com
Hi

I am a newbie for web2py and migrated to python from php. I want to
create my own role based simple authentication system using python
decorators in web2py. When I create a folder myrole and import the
package in db.py (from myrole import *). Web2py throwing exception

Traceback (most recent call last):
File "/home/vikram/Desktop/web2py/gluon/restricted.py", line 188, in
restricted
exec ccode in environment
File "/home/vikram/Desktop/web2py/applications/myapp/models/db.py",
line 8, in <module>
from myrole import *
ImportError: No module named myrole

Can any one help on this issue

Thanks
Vikram

Bruno Rocha

unread,
Dec 9, 2010, 2:20:25 AM12/9/10
to web...@googlegroups.com, susha...@gmail.com
You have to put your module in /web2py/app/modules


from book:


Third Party Modules

import
local_import

web2py is written in Python, so it can import and use any Python module, including third party modules. It just needs to be able to find them. As with any Python application, modules can be installed in the official Python "site-packages" directory, and they can then be imported from anywhere inside your code.

Modules in "site-packages" directory are, as the name suggests, site-level packages. Applications requiring site-packages are not portable unless these modules are installed separately. The advantage of having modules in "site-packages" is that multiple applications can share them. Let's consider, for example, the plotting package called "matplotlib". You can install it from the shell using the PEAK easy_install command:

1.
easy_install py-matplotlib

and then you can import it into any model/controller/view with:

1.
import matplotlib

The web2py source distribution, and the Windows binary distribution has a site-packages in the top-level folder. The Mac binary distribution has a site-packages folder in the folder:

1.
web2py.app/Contents/Resources/site-packages

The problem with using site-packages is that it becomes difficult to use different versions of a single module at the same time, for example there could be two applications but each one uses a different version of the same file. In this example, sys.path cannot be altered because it would affect both applications.

For this kind of situation, web2py provides another way to import modules in such a way that the global sys.path is not altered: by placing them in the "modules" folder of an application. One side benefit is that the module will be automatically copied and distributed with the application; however, there are certain restrictions that apply. web2py provides alocal_import function that must be used to import modules from the "modules" folder. Here is an example of usage:

1.
mymodule = local_import('mymodule')

The function looks for mymodule in the app local modules folder and imports it with the name on the left-hand side of equal.

This function takes three arguments: namereload and app. When you specify reload=True, it will re-import the module upon each request; otherwise your python process will only import the module once. The default is reload=Falseapp is the name of the application from which to import the module, it defaults to request.application.



2010/12/9 Vikram <vikra...@gmail.com>

Vikram

unread,
Dec 9, 2010, 2:55:02 AM12/9/10
to web2py-users
Still its giving the same error. I have placed file checkrole.py under
modules/role/checkrole.py

Here is my code in db.py

role=local_import('role')
db=SQLDB('mysql://root:test123@localhost/web')
db.define_table('address',Field('name'),Field('address'))
db.address.name.requires=IS_NOT_EMPTY()
db.address.address.requires=IS_NOT_EMPTY()

And the Error

Traceback (most recent call last):
File "/home/vikarmt/Desktop/web2py/gluon/restricted.py", line 188,
in restricted
exec ccode in environment
File "/home/vikarmt/Desktop/web2py/applications/myapp/models/db.py",
line 1, in <module>
role=local_import('role')
File "/home/vikarmt/Desktop/web2py/gluon/compileapp.py", line 241,
in <lambda>
local_import_aux(name,reload,app)
File "/home/vikarmt/Desktop/web2py/gluon/compileapp.py", line 172,
in local_import_aux
module = __import__(name)
ImportError: No module named role

Any Idea why the error is coming?

Thanks
Vikram


On Dec 9, 12:20 pm, Bruno Rocha <rochacbr...@gmail.com> wrote:
> You have to put your module in /web2py/app/modules
>
> and use local_importhttp://web2py.com/book/default/chapter/04?search=local_import
> <http://web2py.com/book/default/docstring/local_import>('mymodule')
>
> The function looks for mymodule in the app local modules folder and imports
> it with the name on the left-hand side of equal.
>
> This function takes three arguments: name, reload and app. When you specify
> reload=True, it will re-import the module upon each request; otherwise your
> python process will only import the module once. The default is reload=False
> . app is the name of the application from which to import the module, it
> defaults to request.application.
>
> 2010/12/9 Vikram <vikram.c...@gmail.com>

pbreit

unread,
Dec 9, 2010, 4:43:50 AM12/9/10
to web...@googlegroups.com
It should be:
modules/checkrole.py

and:
role=local_import('checkrole')

Vikram

unread,
Dec 9, 2010, 5:12:30 AM12/9/10
to web2py-users
Thanks a ton...Its working :-)
Reply all
Reply to author
Forward
0 new messages