Reputation: 31
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine("postgresql://postgres:114920@localhost/Databases")
db = scoped_session(sessionmaker(bind=engine))
def main():
Railway = db.execute("SELECT origin, destination, duration FROM Railway").fetchall()
for railway in Railway:
print(f"{railway.origin} to {railway.destination}, {railway.duration} minutes")
if __name__ == "__main__":
main()
and got this error (if possible pls share code I'm stuck at this foe a long time)
C:\Web Development\Lecture02>python hoja1.py Traceback (most recent call last): File "C:\Web Development\Lecture02\hoja1.py", line 6, in engine = create_engine("postgresql://postgres:114920@localhost/Databases") File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine_init_.py", line 500, in create_engine return strategy.create(*args, **kwargs) File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\strategies.py", line 87, in create dbapi = dialect_cls.dbapi(**dbapi_args) File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\dialects\postgresql\psycopg2.py", line 778, in dbapi import psycopg2 ModuleNotFoundError: No module named 'psycopg2'
Upvotes: 0
Views: 1116
Reputation: 31
import os
from sqlalchemy import create_engine from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine("postgresql://postgres:114920@localhost:5432/postgres") db = scoped_session(sessionmaker(bind=engine))
def main(): Railway = db.execute("SELECT origin, destination, duration FROM Railway").fetchall() for railway in Railway: print(f"{railway.origin} to {railway.destination}, {railway.duration} minutes")
if name == "main": main()
Upvotes: 0
Reputation: 26
looks like you dont have psycopg2 module try pip installing psycopg2 run this command in your terminal
pip install psycopg2
Upvotes: 0