Pylons standalone script

29 views
Skip to first unread message

neich

unread,
Sep 5, 2007, 5:45:16 PM9/5/07
to pylons-discuss
Hi,

I am playing with the pylons framework and I have found something I
don't know how to do it. I would like to write a python script that
can access the models of my app, by I want to execute directly, not
through a controller. For example, I would like to have a crontab
process that perform some peridocal operations on my databse.

I have tried some "Import" combinations, but I have not managed to do
it.

Any hint ?

Thanks
Nacho

Ben Bangert

unread,
Sep 5, 2007, 5:53:22 PM9/5/07
to pylons-...@googlegroups.com

You'll need to know where your config ini file is, and load your app
from the script. Your websetup.py script in your project should have
code to do this (in 0.9.6 it does). If it doesn't, you'll need to run
this first before importing your models (again, this works in 0.9.6):

from paste.deploy import appconfig
from pylons import config

from YOURPROJECT.config.environment import load_environment

conf = appconfig('config:' +'/wherever/your/config.ini')
load_environment(conf.global_conf, conf.local_conf)

import YOURPROJECT.model as model

etc.

Cheers,
Ben

Ignacio Martin

unread,
Sep 6, 2007, 11:45:06 AM9/6/07
to pylons-discuss
I answer myself:

----
#!/usr/local/bin/python2.4

from bolsa.websetup import *

setup_config('', '/path_to_your_apps/myapp/development.ini', '', '')

import myapp.model as model

q = model.Session.query(model.MyClass)
# ... do whatever yo want
model.Session.commit()
-----

and don't forget to set your PYTHONPATH env variable to
'/path_to_your_apps/myapp'

Nacho

2007/9/5, neich <nat...@googlemail.com>:

neich

unread,
Sep 7, 2007, 6:43:28 AM9/7/07
to pylons-discuss
Thanks Ben,

That is exacltly what I have done. And I found the 'setup_config'
function that does what you explained.


> You'll need to know where your config ini file is, and load your app
> from the script. Your websetup.py script in your project should have
> code to do this (in 0.9.6 it does). If it doesn't, you'll need to run
> this first before importing your models (again, this works in 0.9.6):
>
> from paste.deploy import appconfig
> from pylons import config
>
> from YOURPROJECT.config.environment import load_environment
>
> conf = appconfig('config:' +'/wherever/your/config.ini')
> load_environment(conf.global_conf, conf.local_conf)
>
> import YOURPROJECT.model as model
>
> etc.
>
> Cheers,
> Ben
>

> smime.p7s
> 3KDownload

Reply all
Reply to author
Forward
0 new messages