Fastapi: [QUESTION] Postgresql template migration error

Created on 14 May 2019  ยท  3Comments  ยท  Source: tiangolo/fastapi

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.

question

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:

PYTHONPATH=. alembic current

or simply set the PYTHONPATH environment variable to be . :)

Best regards,

All 3 comments

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

  1. the alembic upgrade is ran in prestart.sh with PYTHONPATH=. alembic upgrade head
  2. insideenv.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-L30
    Note that env.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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vnwarrior picture vnwarrior  ยท  3Comments

RogerioDosSantos picture RogerioDosSantos  ยท  3Comments

zero0nee picture zero0nee  ยท  3Comments

KoduIsGreat picture KoduIsGreat  ยท  3Comments

DrPyser picture DrPyser  ยท  3Comments