How to load Pyramid environment?

89 views
Skip to first unread message

Gopalakrishnan S

unread,
May 19, 2011, 11:29:30 AM5/19/11
to pylons-discuss
I have a stand-alone python program which needs the pyramid to be
initialised so that it can take use of models, sessions, etc without
serving web request..

Is it possible in Pyramid? I was using the same with pylons, wonder
how Pyramid support this.

Michael

unread,
May 19, 2011, 11:47:05 AM5/19/11
to pylons-...@googlegroups.com
Why would you use Pyramid for this?  Just use SQLALchemy itself.

Kai Groner

unread,
May 19, 2011, 1:00:30 PM5/19/11
to pylons-...@googlegroups.com
This is the template I've been using for my paster commands.

If you don't want the paster stuff, the loadapp and threadlocal_manager bits are the important ones.


Kai


--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To post to this group, send email to pylons-...@googlegroups.com.
To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.


Gopalakrishnan S

unread,
May 20, 2011, 11:54:09 PM5/20/11
to pylons-discuss
OK.. Here is what my problems..

I use celery with pylons.. The following script initialize my pylons
environment so that I would be able to use the project models, lib etc
from outside paster. In my celery task, I use my models defined in the
project..


import pylons
from paste.deploy import appconfig
conf = appconfig('config:' + get_config_file(),
relative_to=relative_path)
load_environment(conf.global_conf, conf.local_conf)


How to do the similar things in pyramid? Please note that I do not
want to serve the web page in this mode..Also I run the celery as a
standalone mode some thing like

> celeryd -B

and the celery load the settings from celeryconfig.py..

I found the following in Mike Orr's migration guide
https://bytebucket.org/sluggo/pyramid-docs/wiki/html/migration.html
Document yet to complete

import logging.config
import os
import paste.deploy.loadwsgi as loadwsgi
ini_path = "/path/to/development.ini"
logging.config.fileConfig(ini_path)
app_dir, ini_file = os.path.split(ini_path)
app = loadwsgi.loadapp("config:" + ini_file, relative_to=app_dir)

So does this initialize pyramid as well? If it does, then I am done
with that..The only difference I am seeing here is that in Pylons, we
call load_environment where in pyramid it seems like calling
loadwsgi.loadapp, hope this will not serve the app..

Thanks,

Gopalakrishnan Subramani

On May 19, 10:00 pm, Kai Groner <k...@gronr.com> wrote:
> This is the template I've been using for my paster commands.https://gist.github.com/981224

Mike Orr

unread,
May 22, 2011, 11:45:14 PM5/22/11
to pylons-...@googlegroups.com
On Fri, May 20, 2011 at 8:54 PM, Gopalakrishnan S
<gopalakrishn...@gmail.com> wrote:
>    import pylons
>    from paste.deploy import appconfig
>    conf = appconfig('config:' + get_config_file(),
> relative_to=relative_path)
>    load_environment(conf.global_conf, conf.local_conf)

Replace the last line with:

from my_pyramid_app import main
app = main(conf.global_conf, **conf.local_conf)

> import logging.config
> import os
> import paste.deploy.loadwsgi as loadwsgi
> ini_path = "/path/to/development.ini"
> logging.config.fileConfig(ini_path)
> app_dir, ini_file = os.path.split(ini_path)
> app = loadwsgi.loadapp("config:" + ini_file, relative_to=app_dir)
>
> So does this initialize pyramid as well? If it does, then I am done
> with that..The only difference I am seeing here is that in Pylons, we
> call load_environment where in pyramid it seems like calling
> loadwsgi.loadapp, hope this will not serve the app..

No, it works the same in both Pylons and Pyramid. The fundamental way
to instantiate a Pyramid application is to call the main() function.
In Pylons there are two levels. load_environment() initializes the
configuration level. make_app() in middleware.py instantiates a
PylonsApp and puts all the middleware around it. Pyramid doesn't have
this distinction: there's only the main() function which returns a
Pyramid application without middleware.

loadwsgi.loadapp() is a Paste wrapper around all this. It reads the
INI section and invokes the entry point indicated by the use= line.
The entry point points to make_app(), so this is just an indirect way
of calling make_app(). In Pyramid, the entry point points to the
main() function, so it's the same thing.

It will not serve the app by itself. You have to instantiate the
server by calling 'server= loadwsgi.loadserver(), and then call
'server(app)'.

--
Mike Orr <slugg...@gmail.com>

Ogi

unread,
May 24, 2011, 8:57:06 PM5/24/11
to pylons-...@googlegroups.com
I tried to follow the approach in this discussion but if I used the development.ini generated by pyramid I got the following result for my conf object.

conf.global_conf:
 {'__file__': '/home/gti-dev/pyramid/Pylons-shootout-77a92a6/development.ini', 'here': '/home/gti-dev/pyramid/Pylons-shootout-77a92a6'}

conf.local_conf:  {}

Only when I follow the same format pylons is using for development.ini then I will get the correct conf.global_conf and conf.local_conf



--

Gopalakrishnan S

unread,
May 26, 2011, 10:12:56 AM5/26/11
to pylons-discuss
Thank you Mike.. It does help..

On May 23, 8:45 am, Mike Orr <sluggos...@gmail.com> wrote:
> On Fri, May 20, 2011 at 8:54 PM, Gopalakrishnan S
>
> Mike Orr <sluggos...@gmail.com>
Reply all
Reply to author
Forward
0 new messages