Mounting / Serving a Pyramid Application From Several Paths / URLs

One of the requirements for an application we’re developing is that the same application should be served from different endpoints. The application currently lives at / at one of our servers, but should also be available at /foo/bar – which is served through a 3rd party varnish with our server as the backend, pulling the application into a different domain name space.

mod_wsgi in Apache supports this, but for those of us who are not using the Apache version, you can also handle it directly in the Pyramid configuration by using the urlmap handler, available in the Paste library.

Our old configuration:

use = egg:applicationname

First, we’ll have to require paste in, so add:


to the requires = [ ... ] list in your (and rerun develop).

Second, use urlmap as your main application in your configuration (development.ini, production.ini). urlmap will then route the request internally to the required application (which also means that the endpoints may point to different applications, but we’re not using that yet):

use = egg:Paste#urlmap
/ = applicationname
/foo/bar = applicationname
/spam/ham = applicationname

use = egg:applicationame

The composite app is now the main entry point for the WSGI application, which then hands the request off to the actual application, applicationname.

Leaking Memory / Cursors with SQLAlchemy and Pyramid

After spending the better part of the day trying to find out why the fsck my console script for importing a dataset through sqlalchemy needed just above 7GBs of memory before barfing out and swapping like a madman, I finally found the solution.

Make sure that Pyramid’s debug toolbar is disabled. It’ll keep an reference around to all queries ran through SQLAlchemy (for .. well, debugging purposes, obviously). This causes an issue if you’re running a very large number of queries, and you’re not going to use the debug toolbar from the console anyway, so .. get rid of it.

I created a second version of my development.ini, a development_console.ini that doesn’t load the debug toolbar, and finally stuff Just Worked ™ again.

Console / shell script runs twice when using pyramid.paster and bootstrapping

You’ve just created your new, exceptional shell script to maintain your pyramid application from the console, when you discover that everything runs twice. Usually not a very good idea, but .. why?

The issue is probably that you’re running pyramid with the scan option, which requires all modules in the path of your pyramid application to be imported. This will also import your console script, and if you haven’t placed everything into a function and added a check to see if the script has been invoked directly, you’re fscked!

The easy way out is to put your code into a function:

def main():
    from pyramid.paster import bootstrap
    env = bootstrap('../../development.ini')


if __name__ == '__main__':  

The last if-test check if this is the main file that has been invoked, and if true, calls main and launches your script. This should hopefully solve the issue!

Pyramid: pkg_resources.DistributionNotFound: <projectname>

When trying to start the built-in WSGI server in Pyramid after starting a new project, pserve refused to do anything useful with my project. Turns out I had forgot to run in my projects virtual environment to set up all the dependencies:

From my projects folder:

    ../bin/python develop

.. of course, you’ll remember this if you read the README.txt that the pyramid setup creates for you in your project directory.