Problem#
When you separate alembic migration source to a separate project, if you donβt want to build a seperate Python package (which can install locally or globally), then you may need to import relatively
Here is solution#
graph TD
root["project root\n(run from terminal here)"]
root --> dbmig["db-migration/\n(alembic)"]
root --> source["source/"]
dbmig --> envpy["env.py\n(imports from source)"]
source --> modelpy["model.py"]
source --> dbpy["db.py\n(imports .db from current dir)"]
envpy -- "from source import model" --> modelpy
note["Run: PYTHONPATH=.. alembic revision --autogenerate -m 'Add users table'\n(go up to parent to resolve imports)"]
Problem 2:#
Problem: Import#
When you run main.py directly (inside source), Python sets the working directory to /source, so the root folder (Unit 06-Authentication/) β which contains db_common β is not in the import path.
Source code
Unit 06-Authentication/ β βββ db_common/ β βββ init.py β βββ db.py β βββ db-migration/ β βββ source/ βββ auth/ β βββ init.py β βββ auth_in_fastapi.py β βββ crud.py β βββ util.py β βββ main.py βββ models.py βββ schema.py βββ docker-compose.yml
Unit 06-Authentication/
Unit06Authentication/ β βββ app/ β βββ init.py β βββ main.py β β β βββ auth/ β β βββ init.py β β βββ auth_in_fastapi.py β β βββ crud.py β β βββ util.py β β β βββ core/ β β βββ init.py β β βββ db.py β β β βββ models.py β βββ schema.py β βββ db_migration/ β βββ docker-compose.yml βββ requirements.txt