模块在Python中没有属性错误
这是我的配置.py模块在Python中没有属性错误,python,Python,这是我的配置.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'
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()
尝试导入sess变量时出错
比如说,在一个单独的模块中,我尝试进行配置。sess,我得到的sess不是一个属性错误。但是配置。基本工作正常
我哪里出错了?我假设您使用的库遵循的惯例是
在重新阅读代码之后:行
sess=Session()
尝试创建类Session的新对象,这不是您想要的。使用sess=Session
您在sess=Session()
上遇到了哪些错误?另外,什么是配置
?它不在您的代码中。configurations.py是我在此处发布的代码的文件名。当我尝试sess=Session()时,我没有收到任何错误,但仍然没有成功。正如我所说:执行导入时,sess变量可能不存在,因此您会收到错误。您可以编写一个get_session()
方法并导入它-只需将会话相关的代码封装在这个方法中。