title: SQLAlchemy best practices: when / how to configure a scoped_session?
body: <p>I am trying to figure out the right approach to use SQLAlchemy scoped sessions the "right way" while keeping the logic of defining a session separate from configuration and separate from using the session. I have been told a number of times a good aproeach would be to have some global scoped_session factory where I can use everywhere: </p>
  
  <pre><code>"""myapp/db.py
  """
  
  from sqlalchemy.orm import sessionmaker, scoped_session
  
  Session = scoped_session(sessionmaker())
  </code></pre>
  
  <p>Then when I want to use it:</p>
  
  <pre><code>"""myapp/service/dosomething.py
  """
  
  from myapp.db import Session
  
  def do_something(data): 
      """Do something with data
      """
      session = Session()
      bars = session.query(Bar).all()
      for bar in bars:
          bar.data = data
      session.commit()
  </code></pre>
  
  <p>This seems right, but my problem is that in all examples I have seen, <code>sessionmaker</code> would also set some parameters of the session, namely and most importantly bind an engine. This makes no sense to me, as the actual DB engine will be created from configuration not known at the global scope during the import of the <code>myapp.db</code> module. </p>
  
  <p>What I have looked at doing is to set everything up in my app's "main" (or in a thread's main function), and then just assume that the session is configured in other places (such as when used by <code>do_something()</code> above):</p>
  
  <pre><code>"""myapp/main.py
  """
  
  from sqlalchemy import create_engine
  from myapp.db import Session
  from myapp.service.dosomething import do_something
  
  def main(): 
      config = load_config_from_file()
      engine = create_engine(**config['db'])
      Session.configure(bind=engine)
  
      do_something(['foo', 'bar'])
  </code></pre>
  
  <p>Does this seem like a correct approach? I have not found any good examples of such a flow yet most other examples I find seem either over-simplified or framework specific. </p>