Run Pylons controller as separate app?

我与影子孤独终老i 提交于 2019-12-03 11:51:32

I'm redacting my response and upvoting the other answer by Ben Bangert, as it's the correct one. I answered and have since learned the correct way (mentioned below). If you really want to, check out the history of this answer to see the wrong (but working) solution I originally proposed.

If you want to load parts of a Pylons app, such as the models from outside Pylons, load the Pylons app in the script first:

from paste.deploy import appconfig
from pylons import config

from YOURPROJ.config.environment import load_environment

conf = appconfig('config:development.ini', relative_to='.')
load_environment(conf.global_conf, conf.local_conf)

That will load the Pylons app, which sets up most of the state so that you can proceed to use the SQLAlchemy models and Session to work with the database.

Note that if your code is using the pylons globals such as request/response/etc then that won't work since they require a request to be in progress to exist.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!