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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0