Hi,
I am getting an issue when I want to use Alembic for migration. the error is as follows:
alembic current
Traceback (most recent call last):
File "/home/lzah/.local/share/virtualenvs/app-211hAVC5/bin/alembic", line 10, in <module>
sys.exit(main())
File "/home/lzah/.local/share/virtualenvs/app-211hAVC5/lib/python3.7/site-packages/alembic/config.py", line 540, in main
CommandLine(prog=prog).main(argv=argv)
File "/home/lzah/.local/share/virtualenvs/app-211hAVC5/lib/python3.7/site-packages/alembic/config.py", line 534, in main
self.run_cmd(cfg, options)
File "/home/lzah/.local/share/virtualenvs/app-211hAVC5/lib/python3.7/site-packages/alembic/config.py", line 514, in run_cmd
**dict((k, getattr(options, k, None)) for k in kwarg)
File "/home/lzah/.local/share/virtualenvs/app-211hAVC5/lib/python3.7/site-packages/alembic/command.py", line 493, in current
script.run_env()
File "/home/lzah/.local/share/virtualenvs/app-211hAVC5/lib/python3.7/site-packages/alembic/script/base.py", line 475, in run_env
util.load_python_file(self.dir, "env.py")
File "/home/lzah/.local/share/virtualenvs/app-211hAVC5/lib/python3.7/site-packages/alembic/util/pyfiles.py", line 90, in load_python_file
module = load_module_py(module_id, path)
File "/home/lzah/.local/share/virtualenvs/app-211hAVC5/lib/python3.7/site-packages/alembic/util/compat.py", line 156, in load_module_py
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "alembic/env.py", line 11, in <module>
from app.db.base import Base
ModuleNotFoundError: No module named 'app'
I tried many different scenarios, yet nothing worked.
it's very hard to respond such a question without a project layout and / or your PYTHONPATH and from where you're running your migrations, because it's all this is about.
having said that I recently pushed something very rough that does a alembic upgrade
just to explain its inner workings (and be sure you can handle things differently of course) the basic layout is:
โโโ app
โย ย โโโ alembic.ini
โย ย โโโ __init__.py
โย ย โโโ main.py
โย ย โโโ migrations
โย ย โย ย โโโ env.py
โย ย โย ย โโโ __init__.py
โย ย โย ย โโโ README
โย ย โย ย โโโ script.py.mako
โย ย โย ย โโโ versions
โย ย โย ย โโโ 5200594f17bc_.py
โย ย โโโ models
โย ย โย ย โโโ __init__.py
โย ย โโโ settings.py
โโโ prestart.sh
alembic upgrade
is ran in prestart.sh
with PYTHONPATH=. alembic upgrade head
env.py
I setup the sqlalchemy.url
with the following lines: https://github.com/euri10/euri10_fastapi_base/blob/033667640920cb69733e9c751f807c530a763816/python3.7-alpine3.8/app/migrations/env.py#L25-L30env.py
is one level below settings.py yet running with PYTHONPATH=.
does the trick...Hope it helps
edit: where is the FastAPI question :grin: ?
Hey,
Thanks for your reply.
It is actually related to FastAPI because I am trying to use Alembic with the Postgresql template. PYTHONPATH=.
worked fine.
I just run every command by putting PYTHONPATH=.
in the beginning like so:
PYTHONPATH=. alembic current
or simply set the PYTHONPATH environment variable to be .
:)
Best regards,
Thanks for your help @euri10 !
@laith43d it seems you are using the project generator, right? You should have a README.md
in your project with details about these steps: https://github.com/tiangolo/full-stack-fastapi-postgresql/tree/master/%7B%7Bcookiecutter.project_slug%7D%7D#migrations
It seems like you were running your migrations locally, not inside Docker.
Anyway, I'm glad you solved your problem. Thanks for reporting back and closing the issue.
Most helpful comment
Hey,
Thanks for your reply.
It is actually related to FastAPI because I am trying to use Alembic with the Postgresql template.
PYTHONPATH=.
worked fine.I just run every command by putting
PYTHONPATH=.
in the beginning like so:or simply set the PYTHONPATH environment variable to be
.
:)Best regards,