当前位置: 首页 > news >正文

FastAPI - Study Notes 5

database.py:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
from sqlalchemy.ext.declarative import declarative_baseDATABASE_URL = 'postgresql://postgres:postgres@localhost/TodoAppDB'engine = create_engine(DATABASE_URL)SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Now we need to create a SessionLocal and each instance of the SessionLocal will have a database session.
# The class itself is not a database session yet. We will add that later on.
# But right now we just need to be able to create an instance of SessionLocal that will be able to become an actual database in the future.

Base = declarative_base()
# create an object of our database which will then be able to interact with the tables that we create in the future.def get_db():db = SessionLocal()try:yield dbfinally:db.close()

 

(fastapi-venv) frank@ZZHPC:~/zproject/TodoApp$ uvicorn main:app --reload
......
ModuleNotFoundError: No module named 'psycopg2'

 

(fastapi-venv) frank@ZZHPC:~/zproject/TodoApp$ dpkg -l libpq-dev
dpkg-query: no packages found matching libpq-dev
(fastapi-venv) frank@ZZHPC:~/zproject/TodoApp$ sudo apt install libpq-dev
[sudo] password for frank: 
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following package was automatically installed and is no longer required:libwayland-server0
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:libpq5
Suggested packages:postgresql-doc-16
The following NEW packages will be installed:libpq-dev libpq5
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 298 kB of archives.
After this operation, 1048 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 libpq5 amd64 16.13-0ubuntu0.24.04.1 [146 kB]
Get:2 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 libpq-dev amd64 16.13-0ubuntu0.24.04.1 [152 kB]
Fetched 298 kB in 3s (87.6 kB/s)  
Selecting previously unselected package libpq5:amd64.
(Reading database ... 59400 files and directories currently installed.)
Preparing to unpack .../libpq5_16.13-0ubuntu0.24.04.1_amd64.deb ...
Unpacking libpq5:amd64 (16.13-0ubuntu0.24.04.1) ...
Selecting previously unselected package libpq-dev.
Preparing to unpack .../libpq-dev_16.13-0ubuntu0.24.04.1_amd64.deb ...
Unpacking libpq-dev (16.13-0ubuntu0.24.04.1) ...
Setting up libpq5:amd64 (16.13-0ubuntu0.24.04.1) ...
Setting up libpq-dev (16.13-0ubuntu0.24.04.1) ...
Processing triggers for libc-bin (2.39-0ubuntu8.7) ...
Processing triggers for man-db (2.12.0-4build2) ...
(fastapi-venv) frank@ZZHPC:~/zproject/TodoApp$ pip install psycopg2
Collecting psycopg2Downloading psycopg2-2.9.11.tar.gz (379 kB)Installing build dependencies ... doneGetting requirements to build wheel ... donePreparing metadata (pyproject.toml) ... done
Building wheels for collected packages: psycopg2Building wheel for psycopg2 (pyproject.toml) ... doneCreated wheel for psycopg2: filename=psycopg2-2.9.11-cp313-cp313-linux_x86_64.whl size=550339 sha256=e8e6b2953ba0dcc8f27764b841935b401e18dd4c20a8376da95efa09f5f625daStored in directory: /home/frank/.cache/pip/wheels/63/ed/1a/7f7f58e98cbe6623951e4308d81a93c8087d1ac9804513a056
Successfully built psycopg2
Installing collected packages: psycopg2
Successfully installed psycopg2-2.9.11