I'm trying to configure tests in the GitHub workflows for my FastAPI application. My alembic.ini
-file is located in app
folder. The alembic ´env.py´ is located in app/alembic
.
This is how I try to run migrations:
- name: Migrate
env:
DB_USER: postgres
DB_HOST: localhost
DB_PASSWORD: postgres
working-directory: app/
run: |
alembic upgrade head
GitHub finds my alembic.ini
file but the problem is, in app/alembic/env.py
I have an import from app.core.config
and that gives me an error:
File "alembic/env.py", line 13, in <module>
from app.core.config import settings # noqa: E402
ModuleNotFoundError: No module named 'app.core'
I've tried setting the directory in the env.py
like this:
from os.path import abspath, dirname
sys.path.insert(0, dirname(dirname(dirname(abspath(__file__)))))
Without this, I get an error ModuleNotFoundError: No module named 'app'
.
Any help would be appreciated.
The solution to my problem was to remove the line sys.path.insert(0, dirname(dirname(dirname(abspath(__file__)))))
and run the migrations like this: PYTHONPATH=. alembic upgrade head
.