I’d like to create a Whoosh index from entries in the database connected to my Pyramid application. However, I’m not really sure how to access the database outside of application.
So my models.py is initialized as follows:
from sqlalchemy import (
Column,
Integer,
Text,
String,
ForeignKey,
Table
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import (
scoped_session,
sessionmaker,
relationship,
backref
)
from sqlalchemy.dialects.mysql import DATETIME, FLOAT, TEXT
from zope.sqlalchemy import ZopeTransactionExtension
db_session = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
dbBase = declarative_base()
dbBase.query = db_session.query_property()
Then in __init__.py, there is an example of loading in the models:
from pyramid.config import Configurator
from sqlalchemy import engine_from_config
from .models import db_session, Recipe
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
engine = engine_from_config(settings, 'sqlalchemy.')
db_session.configure(bind=engine)
my production.ini has the engine assignment:
sqlalchemy.url = mysql+pymysql://username:password@localhost:3306/database?charset=utf8
So main is called when the WSGI process is started, which passes the engine from the .ini file. But I’d like to access the database through a script that does not rely on the WSGI process. Can I just assign the engine and bind it to the session in the script? How does the extension=ZopeTransactionExtension() affect the session?
There is a section in the Pyramid documentation which deals with writing scripts, however it’s buried in the Command-Line section. The pertinent part is that initializedb.py has been converted into a console script, which creates a script in the
bindirectory. This is whymodelsis imported using relative importing.This seemed a bit superflous for my needs at the moment, so I still needed something simpler. The solution was to include:
in my script and then call the script from the directory containing my
production.inifile with:The
-mruns the module as a script. This fixes the relative importing, thereby employing all the benefits of the predefinedinitializedb.pyscript.