How can I use modules in web2py?
Asked Answered
H

2

20

I have some functions in func.py that I would like to access from my web2py controller called default.py. I tried to import and use func.py in default.py with "import func" and "calculatesomething = func.calculatesomething", but it doesn't work. The file func.py is also added in the Modules section of my web2py program.

Why it is not working? How can I make it to work? Should the module be in func.py or default/func.py or something else?

Haberdasher answered 11/1, 2011 at 12:14 Comment(0)
T
28

func has to be in a place that is in PYTHONPATH (sys.path) such as web2py/site-packages. This is the proper Pythonic way of doing it.

If instead you want to package your module with your app, then place it in applications/yourapp/modules, next, you should import it with func = local_import('func')

Q: People who are new to web2py may wonder: why use local_import() when the former method is the standard one in the Python world?

A: Because in web2py you can install new apps without restarting the server. Apps cannot change sys.path because it is not thread safe and would depend on the order those apps are installed. Moreover two apps may require modules with the same name but different (for example different versions). We want to avoid conflicts between those modules and other modules that may be pre-installed.

local_import() is a workaround that allows you to ship some modules with your apps and import them without adding the folder to sys.path and causing version conflicts.

Toney answered 11/1, 2011 at 15:9 Comment(0)
C
38

Just to add a bit to user570039's answer, local_import is documented here. It includes a reload parameter. According to the documentation:

When you specifyreload=True, it will re-import the module upon each request; otherwise your python process will only import the module once. The default isreload=False.

In development, setting reload=True can be convenient because changes to your module will work immediately without requiring a restart. However, re-importing upon each request will slow down performance, so reload=False is recommended in production.


UPDATE: Things have changed. local_import has been deprecated. If you have /applications/myapp/modules/mymodule.py, you can import it within myapp by doing:

import mymodule

web2py will check the application's "modules" folder before checking the rest of sys.path.

For more details, see here.

Chivalric answered 13/1, 2011 at 2:27 Comment(8)
Thank you, guys! Both of these answers were very helpful!Haberdasher
How can I do this with folders? For example, when I'm trying to import modules called fuzz and process in the fuzzywuzzy library from /applications/myapp/modules/fuzzywuzzy/ directory, using from fuzzywuzzy import fuzz, process on GAE with Py2.7, I'm getting an ImportError saying <type 'exceptions.ImportError'> No module named fuzzywuzzyImena
I placed it in web2py/site-packages/fuzzywuzzy and am able to import them now :) ... But I still am interested in the possibility of having the module within the web2py/applications/myapp/modules/ directory, especially because I don't want to accidentally break my older apps when I 'update' my modules for newer apps.Imena
For some reason I'm getting an ImportError even for the example above, i.e. web2py/applications/myapp/modules/func.py with import func, in my model db.py on Web2Py 1.99.7, on both GAE and when running web2py.py (Although, it works fine when func.py is placed in web2py/site-packages/)Imena
Does your /modules folder contain an empty __init__.py file?Chivalric
It didn't have it at first, but I still get the same ImportError even after placing it. (Tested on: GAE, PyPy and Python27)Imena
web2py should also work if you put it in the modules folder and from applications.myapp.modules.fuzzywuzzy import fuzz, processFormularize
Since the original answer mentions reload=True, I think for a complete answer the update should include mention of from gluon.custom_import import track_changes; track_changes(True) as a replacement for reload=True functionalityPrase
T
28

func has to be in a place that is in PYTHONPATH (sys.path) such as web2py/site-packages. This is the proper Pythonic way of doing it.

If instead you want to package your module with your app, then place it in applications/yourapp/modules, next, you should import it with func = local_import('func')

Q: People who are new to web2py may wonder: why use local_import() when the former method is the standard one in the Python world?

A: Because in web2py you can install new apps without restarting the server. Apps cannot change sys.path because it is not thread safe and would depend on the order those apps are installed. Moreover two apps may require modules with the same name but different (for example different versions). We want to avoid conflicts between those modules and other modules that may be pre-installed.

local_import() is a workaround that allows you to ship some modules with your apps and import them without adding the folder to sys.path and causing version conflicts.

Toney answered 11/1, 2011 at 15:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.