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