Importing app when using Alembic raises ImportError Importing app when using Alembic raises ImportError flask flask

Importing app when using Alembic raises ImportError


alembic just tries to load your env.py source code. It's not in your package, so it can't access your app module.

Use solution 2 @tomasz-jakub-rup suggested, you can execute like

$ PYTHONPATH=. alembic upgrade head

and you should get your result.


I guess you are trying to run

python env.py

In this case, your app directory is not in PYTHONPATH.

solution 1

Run the app from parent dir:

python alembic/env.py

solution 2

Set the PYTHONPATH before running the app

PYTHONPATH=/path/to/parent/dir python env.py

edit

I read about alembic. As @mrorno said, just set the PYTHONPATH before running alembic:

PYTHONPATH=. alembic upgrade head


Create file .env and insert PYTHONPATH=.