Search code examples
pythonpython-2.7web2pyweb2py-modules

Pattern to get rid of imports in modules as in web2py controllers


I am new to web2py and python both. I am writing a sample blog app in this framework. I want to split the business logic that gets called in each controller method to it's own module, and found this example helpful:

As you can see, you need to import objects in modules or set them through globals.current. The controller can refer to "db" and "request" instances (for example) without any import. What kind of coding mechanism makes it possible in controller but not elesewhere?


Solution

  • Found the answer: Looks like how web2py works is by compiling the python code for the controllers and models and views on the fly. It runs them in it's special 'environment' Related snippets of code are: