我正在尝试将我的Flask项目与Alembic集成
我的应用程序结构如下所示
project/
configuration/
__init__.py
dev.py
test.py
core/
# all source code
db/
migrations/
__init__.py
alembic.ini
env.py
versions/当我尝试从我的db目录运行以下命令时,我看到
File "migration/env.py", line 55, in run_migrations_online
from configuration import app, db
ImportError: No module named configuration我尝试了Request a simple alembic working example for Auto Generating Migrations中提到的解决方案,但它对我不起作用
我在env.py run_migrations_online() with change中的方法是
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
import os
import sys
sys.path.append(os.getcwd())
from configuration import app, db
alembic_config = config.get_section(config.config_ini_section)
alembic_config['sqlalchemy.url'] = app.config['SQLALCHEMY_DATABASE_URI']
target_metadata = db.metadata
engine = engine_from_config(
alembic_config,
prefix='sqlalchemy.',
poolclass=pool.NullPool)
connection = engine.connect()
context.configure(
connection=connection,
target_metadata=target_metadata
)
try:
with context.begin_transaction():
context.run_migrations()
finally:
connection.close()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()我该如何解决这个问题呢?
发布于 2013-04-03 00:23:00
我执行export PYTHONPATH=<path_to_project>并再次运行该命令,它成功运行
发布于 2013-04-01 05:22:06
您说您从目录project/db运行类似于alembic migrate --autogenerate -m 'migration description'的命令并获得ImportError,对吗?
如果是这样,问题就显而易见了。
请参见:您尝试导入configuration模块,但它会导致错误。然后放入sys.path.append(os.getcwd()) -换句话说,将当前目录添加到系统路径。但是当前的目录是什么呢?它是project/db,并且它下面没有configuration模块,所以您可以继续获取ImportError。
解决方案是在系统路径父目录中添加包含configuration模块的project。如下所示:
parent_dir = os.path.abspath(os.path.join(os.getcwd(), ".."))
sys.path.append(parent_dir)发布于 2013-03-31 05:18:13
我们遇到了同样的问题,归根结底,除非设置了 --autogenerate标志,否则修订版不会调用env.py。您可以通过在env.py文件的顶部放置一条print语句来测试这一点。
我们可以通过调用--autogenerate,然后删除生成的代码来解决这个问题。
https://stackoverflow.com/questions/15648284
复制相似问题