Skip to main content

Appendix-02.Relative-Module-Import

Problem

When you seperate alembic migration source to seperate 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

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