Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
模块在Python中没有属性错误_Python - Fatal编程技术网

模块在Python中没有属性错误

模块在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'

这是我的配置.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()
尝试导入sess变量时出错

比如说,在一个单独的模块中,我尝试进行配置。sess,我得到的sess不是一个属性错误。但是配置。基本工作正常


我哪里出错了?

我假设您使用的库遵循的惯例是

  • 方法和属性以小写字母开头
  • 课程以大写字母开头
  • 记住这一点,您的变量没有遵循此约定(这无关紧要,这不是错误,但有时它有助于遵循约定)

    关于你的问题: sess正在引用类Session()的新创建对象。导入时它可能不在那里。 另一方面,Base似乎是一种方法,因此您正在导入一种方法


    在重新阅读代码之后:行
    sess=Session()
    尝试创建类Session的新对象,这不是您想要的。使用
    sess=Session

    您在
    sess=Session()
    上遇到了哪些错误?另外,什么是
    配置
    ?它不在您的代码中。configurations.py是我在此处发布的代码的文件名。当我尝试sess=Session()时,我没有收到任何错误,但仍然没有成功。正如我所说:执行导入时,sess变量可能不存在,因此您会收到错误。您可以编写一个
    get_session()
    方法并导入它-只需将会话相关的代码封装在这个方法中。