I have 2 separate controllers and I need import a function from one controller to the other.
For example, I have a.py and b.py, in b.py I have tried:
import a
from applications.a.modules import a
import applications.a.modules.othermodule
I cannot seem to get anything to work after having looked at the documentation as well: http://web2py.com/book/default/chapter/04#Cooperation
How do I import one a function from one controller to another controller?
In web2py, controllers are not meant to be treated like Python modules and imported in other controllers. Technically you can do that (make sure there is an
__init__.pyfile in the/controllersdirectory), but controller files are meant to be executed per HTTP request in a web2py environment. If you import a function from a controller, calling the function may not work properly because it may reference web2py environment objects (e.g.,request,session, objects defined in model files) that will not be available in the context of the import.If you want to share functionality between controllers, you are much better off moving the shared functionality to a Python module and importing it in each controller that needs it.