Latest Version: 0.9.6.2
  Dashboard > Pylons Cookbook > ... > Database > Accessing your model outside of Pylons
  Pylons Cookbook Log In | Sign Up   View a printable version of the current page.  
  Accessing your model outside of Pylons
Added by Brendan Arnold, last edited by Brendan Arnold on Sep 08, 2007  (view change)
Labels: 

If you just quickly want to fiddle with your model in a Python shell use the paster shell. On the commandline cd to your Pylons application directory and type

paster shell

This will load your model as model

If however you want to include your model in your standalone scripts then it is probably best to write a small module.

Decide where you want your module to live, for example /home/brendan/my_modules and create a folder called pylonsmodel. To put this module on the Python module path, create a file named brendans_modules.pth in a directory already in your module path and add the single line /home/brendan/my_modules. Create another file in the same directory called my_pylons_app.pth and put in that the path to your Pylons application directory.

Finding your module path

Import sys.path to find a list of module paths. You can use this to check that the directory is included in the paths.

Alternate method

If you prefer, you can import sys.path then append paths to using sys.path.append('MY_PATH') rather than creating .pth files. You must do this for each script.

In the pylonsmodel folder create a file named __init__.py and enter the following, replacing MYAPP with your app name and PATH_TO_DEVELOPMENT.INI with the absolute path to your development.ini

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
"""Import the TCD_sample_database model into your script"""

from paste.deploy import appconfig
from pylons import config

from MYAPP.config.environment import load_environment

conf = appconfig('config:' + 'PATH_TO_DEVELOPMENT.INI')
load_environment(conf.global_conf, conf.local_conf)

from MYAPP.model import *

Now you can use this in your scripts as follows,

1
2
3
4
#!/usr/bin/python

import pylonsmodel as model
...

Now your models are available under the model object just as in Pylons.

Reference

http://groups.google.com/group/pylons-discuss/browse_thread/thread/90c47dc6120fa43d/52f25a08f7234fa7?lnk=raot

Site running on a free Atlassian Confluence Open Source Project License granted to Pylons. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.3.3 Build:#645 Feb 13, 2007) - Bug/feature request - Contact Administrators
Top