tags:

views:

153

answers:

2

hi together,

how can i import other external libraries in web2py? is it possible to load up libs in the static file? can somebody give me an example? thanks

peter

A: 

In web2py you import external library as you normally do in Python

import module_name

or

from module_name import object_name

I am not sure what you mean by "in the static file"

mdipierro
+2  A: 

If the library is shipped with python, then you can just use import as you would do in regular python script. You can place your import statements into your models, controllers and views, as well as your own python modules (stored in modules folder). For example, I often use traceback module to log stack traces in my controllers:

import traceback

def myaction():
    try:
        ...
    except Exception as exc:
        logging.error(traceback.format_exc())
        return dict(error=str(exc))

If the library is not shipped with python (for example, pyodbc), then you will have to install that library (using distutils or easy_install or pip) so that python can find it and run web2py from the source code: python web2py.py. Then you will be able to use regular import statement as described above. Before you do this make sure you installed the library properly: run python interpreter and type "import library_name". If you don't get any errors you are good to go.

If you have a third party python module or package, you can place it to modules folder and import it as shown below:

mymodule = local_import('module_name')

You can also force web2py to reload the module every time local_import is executed by setting reload option:

mymodule = local_import('module_name', reload=True)

See http://web2py.com/book/default/section/4/18?search=site-packages for more information.

Roman Bataev