On Wednesday, June 29, 2011 1:08:19 PM UTC-4, Nick Arnett wrote: 
>
> I'm just getting to know Web2Py after working with Django a fair bit.  I 
> have robots that gather data that I would like to insert using the Web2Py 
> ORM, but I can't quite figure out the right way to do that.  If I put the 
> bot scripts in the modules directory, I don't seem to have access to the 
> ORM.

 
Only web2py model, controller, and view files have access to the web2py 
globals -- in a module, you have to explicitly import web2py stuff. You 
could do:
 
from gluon import *   # to get all the web2py globals
 
or
 
from gluon.dal import DAL, Field  # for just the DAL stuff
 
 
If you're importing from outside web2py, make sure the web2py folder is in 
your sys.path. Or, since the DAL can be used independently of the web2py 
framework, you could just copy the /gluon/dal.py file to wherever you want 
to use it.
 

>   When I run a module in the controllers directory, I can import from the 
> modules directory with a simple import even though the documentation seems 
> to say that I should use local_import... which doesn't work.

 
Yes, as of 1.96.1, you can use regular Python import statements to import 
from the app's /modules folder, and local_import is deprecated (and I think 
may not work if your modules import other modules).
 

>  And running a module in the controllers directory doesn't seem to give it 
> access to the ORM, either.

 
No, just putting a module in the controllers directory wouldn't give it 
access to the DAL or any web2py globals. When a request comes in, the 
particular controller requested is executed in an environment with all the 
web2py globals defined, but that wouldn't apply to a module dumped in the 
controllers folder.
 

>  
> So... I'm lost and not finding the answer in the docs.  In Django, I would 
> just put the appropriate imports in the robot scripts, but I get that that's 
> not the Web2Py way!
>
 
If you're not creating an app that receives requests and just want to use 
the DAL as a module with your robot scripts, you can probably just pull out 
the dal.py file and use it as a standalone Python module, importing as 
usual.
 
Anthony
 

Reply via email to