我是全新使用sqlalchemy
和postgresql
。我讀this教程構建下面的一段代碼:'模塊'對象無法用sqlalchemy調用
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy import engine
def connect(user, password, db, host='localhost', port=5432):
'''Returns a connection and a metadata object'''
# We connect with the help of the PostgreSQL URL
# postgresql://federer:[email protected]:5432/tennis
url = 'postgresql://{}:{}@{}:{}/{}'
url = url.format(user, password, host, port, db)
# The return value of create_engine() is our connection object
con = sqlalchemy.create_engine(url, client_encoding='utf8')
# We then bind the connection to MetaData()
meta = sqlalchemy.MetaData(bind=con, reflect=True)
return con, meta
con, meta = connect('federer', 'grandestslam', 'tennis')
con
engine('postgresql://federer:***@localhost:5432/tennis')
meta
MetaData(bind=Engine('postgresql://federer:***@localhost:5432/tennis'))
當運行它,我有這樣的錯誤:
File "test.py", line 22, in <module>
engine('postgresql://federer:***@localhost:5432/tennis')
TypeError: 'module' object is not callable
我該怎麼辦?謝謝 !
Did you import Engine?它來自哪裏? –
@DanielRoseman對不起。我搞砸了,我發佈了一個已經解決的問題。我用真正的問題編輯了我的帖子。 –
你是否也是Python的新手?你顯示的錯誤是不言自明的; 'engine'是一個模塊,你不能調用它。我不明白你在做什麼。 –