Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8688007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:15:20+00:00 2026-06-12T23:15:20+00:00

I’d like to create a Whoosh index from entries in the database connected to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:15:21+00:00Added an answer on June 12, 2026 at 11:15 pm

    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 bin directory. This is why models is 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:

    if __name__ == '__main__':
        main()
    

    in my script and then call the script from the directory containing my production.ini file with:

    ../bin/python -m myproject.scripts.whooshindex production.ini 
    

    The -m runs the module as a script. This fixes the relative importing, thereby employing all the benefits of the predefined initializedb.py script.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a view passing on information from a database: def serve_article(request, id): served_article
Let's say I'm outputting a post title and in our database, it's Hello Y’all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.