0
votes
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'

2
Hi, you are missing psycopg2 module which is sqlalchemy using for interacting with postgresql. Do you have it installed? pip install psycopg2Jan Lipovský

2 Answers

0
votes

looks like you dont have psycopg2 module try pip installing psycopg2 run this command in your terminal

pip install psycopg2
0
votes
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()