Now I understand. I never thought about this before.

Create your file my_script.py and in it you write:

import os
os.chdir('/path/to/web2py/')
from gluon.shell import env
globals().update(env('you_app_name', import_models=True))
# now do whatever you need you can use request, db, helpers, etc.

Massimo

On Aug 31, 7:45 pm, Richard <richar...@gmail.com> wrote:
> I want to have a standalone script to do some operations on my
> application database. And I want to use the web2py DAL so my script is
> as portable as my application.
>
> I have such a script working, which I run with:> cd /home/web2py && python 
> web2py.py --import_models --no-cron --shell=test --run=my_script.py
>
> This feels awkward so I want to run my script with just:
>
> > python my_script.py
>
> How can I do that? It seems exec_environment is not the way.
> In the previous post I was trying to import my model file.
>
> Richard
>
> On Aug 31, 10:54 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > Sorry, I do not understand the context
>
> > If a piece of code needs to be "imported" than it should be a module
> > and go in modules.
>
> > models instead should not be imported and they are always executed
> > alphabetically for every app.
>
> > Massimo
>
> > On Aug 30, 7:08 pm, Richard <richar...@gmail.com> wrote:
>
> > > hi Massimo,
>
> > > didn't realize exec_environment was not recommended. Perhaps that
> > > could be mentioned in the manual (p 125).
>
> > > How would you recommend I import my model? I created models/
> > > __init__.py and then tried:
>
> > > >>> from gluon.sql import SQLDB
> > > >>> from applications.test.models import db
>
> > > which threw "NameError: name 'SQLDB' is not defined"
>
> > > Richard
>
> > > On Aug 28, 6:07 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > > > Althought exec_environment exists I think of it more of workaround
> > > > than something that should really be used.
>
> > > > If your my_script.py needs to call a certain function that function
> > > > (f) belongs to a model, not a controller. If you put in model you do
> > > > not need exec_environment and you can just call it. You can still
> > > > expose it in a controller (def f(): return f())/
>
> > > > The other issue many be Auth. You should not call from script a
> > > > function that requires login because your script probably does not do
> > > > logs in.
>
> > > > Massimo
>
> > > > On Aug 28, 2:22 am, Richard <richar...@gmail.com> wrote:
>
> > > > > hello,
>
> > > > > I want to create a script that interacts with my web2py app, and which
> > > > > I can call with just "python my_script.py"
> > > > > Until now I have been using:
>
> > > > > > cd /home/web2py && python web2py.py --import_models --no-cron 
> > > > > > --shell=test --run=applications/test/modules/my_script.py
>
> > > > > I tried with exec_environment() but Auth is giving me trouble, which
> > > > > seems to require a real request object.
> > > > > This is the error I get:
>
> > > > >   File "applications/test/modules/my_script.py", line 2, in <module>
> > > > >     test = exec_environment('applications/test/models/db.py')
> > > > >   File "/home/web2py/gluon/shell.py", line 63, in exec_environment
> > > > >     execfile(pyfile, env)
> > > > >   File "applications/test/models/db.py", line 18, in <module>
> > > > >     auth = Auth(globals(), db)
> > > > >   File "/home/web2py/gluon/tools.py", line 327, in __init__
> > > > >     self.settings.login_url = self.url('user', args='login')
> > > > >   File "/home/web2py/gluon/tools.py", line 289, in url
> > > > >     f=f, args=args, vars=vars)
> > > > >   File "/home/web2py/gluon/html.py", line 175, in URL
> > > > >     raise SyntaxError, 'not enough information to build the url'
>
> > > > > So, is there a way to create dummy Request objects that will satisfy
> > > > > Auth?
> > > > > Or is there perhaps a better way to create a standalone script?
>
> > > > > Richard
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to