Alembic: alembic revision says Import Error

拟墨画扇 提交于 2019-11-30 08:22:07

I did export PYTHONPATH=<path_to_project> and ran the command again and it ran successfully

You say you run something like alembic migrate --autogenerate -m 'migration description' from the directory project/db and get ImportError, right?

If so, the problem is obvious.

See: you try to import configuration module and it results in errors. Then you put sys.path.append(os.getcwd()) - in other words, you add current directory to the system path. But what is the current directory? It's project/db, and it doesn't have configuration module under it, so you continue getting ImportError.

Solution is to add to system path parent directory - project, which contains configuration module. Like so:

parent_dir = os.path.abspath(os.path.join(os.getcwd(), ".."))
sys.path.append(parent_dir)

We've run into the same problem, it boils down to env.py not being called by revision unless the --autogenerate flag is set. You can test this by putting a print statement at the top of your env.py file.

We're working around it by calling with --autogenerate then removing the generated code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!