0

This is my configurations.py

import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import psycopg2

db_username = 'postgres'
db_password = 'postgres'
db_host = 'localhost'
db_name = 'testdb6'

engine = create_engine('postgresql+psycopg2://' + db_username + ':' + db_password + '@' + db_host +'/' + db_name,echo=True)

Session = sessionmaker()

Session.configure(bind=engine)

sess = Session()

Base = declarative_base()

I get an error when I try to import the sess variable.

Say, in a separate module, I try to do configurations.sess, I get a sess is not an attribute error. But configurations.Base works fine.

Where am I going wrong?

2
  • Which error are you getting on sess = Session()? Also, what is configurations? It's not in your code. Commented Sep 23, 2013 at 9:58
  • configurations.py is the filename of the code I've posted here. I'm getting no errors when I do sess = Session() Commented Sep 23, 2013 at 9:59

1 Answer 1

1

I assume the libraries you are using are following hte convention that

  1. methods and attributes are starting with a lowercase letter
  2. Classes are starting with an uppercase letter.

Keeping this in mind, your variables are not following this convention (which does not matter, it's not an error, but sometimes it helps following conventions).

Coming to your question: sess is referring to a newly created object of class Session(). It just might not be there when you are importing. On the other hand, Base seems to be a method - and therefore you are imorting a method.

And after re-reading your code: The line sess = Session() tries to create a new object of class session - which is not what you want. Use sess = Session.

Sign up to request clarification or add additional context in comments.

2 Comments

Tried it, but still no luck.
As I said: The sess-Variable might just not me existing when the import is executed - therefore you get the error. You could write a get_session() method and import it - just encapsulate the session-related code in this method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.